从 Json 对象获取字符串值 Android [英] Getting String Value from Json Object Android

查看:32
本文介绍了从 Json 对象获取字符串值 Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 初学者.在我的项目中,我从 HTTP 响应中获取以下 json.

I am beginner in Android. In my Project, I am getting the Following json from the HTTP Response.

[{"Date":"2012-1-4T00:00:00",
"keywords":null,
"NeededString":"this is the sample string I am needed for my project",
"others":"not needed"}]

我想从上面的 json 中获取NeededString".如何获得?

I want to get the "NeededString" from the above json. How to get it?

推荐答案

这可能对您有所帮助.

Java:

JSONArray arr = new JSONArray(result);
JSONObject jObj = arr.getJSONObject(0);
String date = jObj.getString("NeededString");

Kotlin:

val jsonArray = JSONArray(result)
val jsonObject: JSONObject = jsonArray.getJSONObject(0)
val date= jsonObject.get("NeededString")

  • getJSONObject(index).在上面的例子中,0 代表索引.
  • 这篇关于从 Json 对象获取字符串值 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆