无法正确解析JSON数组

时间:2015-01-29 10:09:27

标签: java android json

我收到以下来自webserveice的String回复:

[
    [
        {
            "dgtype": "adhoc",
            "subtypename": "Person",
            "subtypedesc": "null",
            "summary": "Junaid (Self)",
            "subtype": "person",
            "birthdate": "1995-1-23 ",
            "name": "Junaid (Self)"
        },
        {
            "dgtype": "adhoc",
            "subtypename": "Job",
            "subtypedesc": "null",
            "summary": "Exa",
            "subtype": "person",
            "birthdate": "2010-01-30",
            "name": "Junaid (Self)"
        }
    ]
]

在Java中,我尝试执行以下操作:

JSONArray jArray = new JSONArray(result);  

                System.out.println("Response: "+jArray);

                for(int i = 0; i<= jArray.length(); i++){
                    try {
                        JSONObject oneObject = jArray.getJSONObject(i);

                        String dgtype = oneObject.getString("dgtype");
                        String subtypename = oneObject.getString("subtypename");
                        String subtypedesc = oneObject.getString("subtypedesc");
                        String summary = oneObject.getString("summary");
                        String subtype = oneObject.getString("subtype");
                        String birthdate = oneObject.getString("birthdate");
                        String name = oneObject.getString("name");

                        System.out.println(i);
                        System.out.println("dgtype: "+dgtype);
                        System.out.println("subtypename: "+subtypename);
                        System.out.println("subtypedesc: "+subtypedesc);
                        System.out.println("summary: "+summary);
                        System.out.println("subtype: "+subtype);
                        System.out.println("birthdate: "+birthdate);
                        System.out.println("name: "+name);




                    } catch (JSONException e) {
                        System.out.println("JSON Exception: "+e);
                    } 
                }

但是我得到以下例外:

JSON Exception: org.json.JSONException: Value  
[
    {
        "dgtype": "adhoc",
        "subtypename": "Person",
        "subtypedesc": "null",
        "summary": "Junaid (Self)",
        "subtype": "person",
        "birthdate": "1995-1-23 ",
        "name": "Junaid (Self)"
    },
    {
        "dgtype": "adhoc",
        "subtypename": "Job",
        "subtypedesc": "null",
        "summary": "Exa",
        "subtype": "person",
        "birthdate": "2010-01-30",
        "name": "Junaid (Self)"
    }
]
at 0 of type org.json.JSONArray cannot be converted to JSONObject
JSON Exception: org.json.JSONException: Index 1 out of range [0..1)

我正在关注this示例。我哪里错了?另请注意异常代码段中缺少的长括号。

4 个答案:

答案 0 :(得分:5)

你有两个阵列,一个在另一个阵列中:

[
   [
    //Your objects
   ]
]

您可以更改数据格式,使其只有一个数组,或修改代码:

JSONArray outer = new JSONArray(result);  
JSONArray jArray = outer.getJSONArray(0);

答案 1 :(得分:1)

jArray JSONArray包含另一个包含JSONArray的{​​{1}},因此首先获取JSONArray,然后从中获取所有JSONObject:

JSONObeject

答案 2 :(得分:1)

正如它所说,&#39; JSONArray无法转换为JSONObject&#39;,你在另一个数组中有一个数组,所以

JSONArray jArray1 = new JSONArray(result);  

然后

JSONArray jArray =  jArray1.getJSONArray(0);

现在它会起作用。

答案 3 :(得分:0)

              for(int i = 0; i<= jArray.length(); i++){
                  final JSONArray innerArray = jArray.getJSONArray(i);
                  for (int a = 0; a < innerArray.length(); a++) { 
                     try {
                        final JSONObject oneObject = innerArray.getJSONObject(i);    
                        String dgtype = oneObject.getString("dgtype");
                        String subtypename = oneObject.getString("subtypename");
                        String subtypedesc = oneObject.getString("subtypedesc");
                        String summary = oneObject.getString("summary");
                        String subtype = oneObject.getString("subtype");
                        String birthdate = oneObject.getString("birthdate");
                        String name = oneObject.getString("name");

                        System.out.println(i);
                        System.out.println("dgtype: "+dgtype);
                        System.out.println("subtypename: "+subtypename);
                        System.out.println("subtypedesc: "+subtypedesc);
                        System.out.println("summary: "+summary);
                        System.out.println("subtype: "+subtype);
                        System.out.println("birthdate: "+birthdate);
                        System.out.println("name: "+name);
                    } catch (JSONException e) {
                        System.out.println("JSON Exception: "+e);
                    } 
                }
             }