Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中解析json字符串?_Android_Json - Fatal编程技术网

如何在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()