如何在Android中解析json字符串?
可能重复:如何在Android中解析json字符串?,android,json,Android,Json,可能重复: 我正在从服务器获取JSON字符串,我已经通过代码获取了JSON字符串。 但我不明白如何解析它 下面是我的JSON字符串 { "university": { "name": "oxford", "url": "http://www.youtube.com" }, "1": { "id": "2", "title": "Baseball", "datetime": "2011-11-
我正在从服务器获取JSON字符串,我已经通过代码获取了JSON字符串。 但我不明白如何解析它 下面是我的JSON字符串
{
"university": {
"name": "oxford",
"url": "http://www.youtube.com"
},
"1": {
"id": "2",
"title": "Baseball",
"datetime": "2011-11-11 10:41:46"
},
"2": {
"id": "1",
"title": "Two basketball team players earn all state honors",
"datetime": "2011-11-11 10:40:57"
}
}
请提供任何指导或代码片段。使用JSON类进行解析,例如
JSONObject mainObject = new JSONObject(Your_Sring_data);
JSONObject uniObject = mainObject.getJSONObject("university");
String uniName = uniObject.getString("name");
String uniURL = uniObject.getString("url");
JSONObject oneObject = mainObject.getJSONObject("1");
String id = oneObject.getString("id");
....
下面是在android中解析JSON字符串的指南链接 此外,根据您的json字符串,代码片段必须如下所示:-
JSONObject mainObject = new JSONObject(yourstring);
JSONObject universityObject = mainObject.getJSONObject("university");
JSONString name = universityObject.getString("name");
JSONString url = universityObject.getString("url");
以下是JSOnObject的API参考:
其他对象也是如此。我们可以得到json字符串中的多少个对象吗?是的,在获取字符串值时,您可以简单地使用“mainObject.length();”编辑为使用
getString()
而不是getJSONObject()
。当我运行类似的代码时,使用getJSONObject()
导致了一个JSONException,这是通过使用getString()
getString()
修复的,而不是示例中编写的getJsonString()
,对吗?对@de。它应该是getString(),而不是getJsonString()