解析JSON多个对象

时间:2019-10-01 06:27:07

标签: java json parsing

我正在尝试解析以下json,但是由于出现堆栈溢出错误而无法做到这一点。

这是JSON-

[{
    "Class": "1",
    "school": "test",
    "description": "test",
    "student": [
        "Student1",
        "Student2"
    ],
    "qualify": true,
    "annualFee": 3.00
}]

这是当前失败的代码。

String res  = cspResponse.prettyPrint();
org.json.JSONObject obj = new org.json.JSONObject(res);
org.json.JSONArray arr = obj.getJSONArray(arrayName);
String dataStatus=null;

for (int i = 0; i < arr.length(); i++) {
    dataStatus = arr.getJSONObject(i).getString(key);
    System.out.println("dataStatus is \t" + dataStatus);
}

用例是:

  1. 获取值键“类”
  2. 从学生那里获得价值
  3. 从学校获得价值

感谢您的帮助。

更新1 使用以下详细信息更新了有关堆栈跟踪的更多信息的代码。 cls = 1

错误-org.json.JSONException: JSONObject["student "] not a string.

堆栈跟踪-

public String getString(String key) throws JSONException {
        Object object = this.get(key);
        if (object instanceof String) {
            return (String) object;
        }
        throw new JSONException("JSONObject[" + quote(key) + "] not a string.");
    }

当我用以下答案运行代码时,这里对学生的失败不是字符串。

我从前两个注释中使用的答案都具有相同的错误。我请你帮忙。

4 个答案:

答案 0 :(得分:5)

您的json片段无效-最后一个逗号中断了解析。但是其余的代码是相当可行的。

    String res = "[\n" +
            "    {\n" +
            "        \"Class\": \"1\",\n" +
            "        \"school\": \"test\",\n" +
            "        \"description\": \"test\",\n" +
            "        \"student\": [\n" +
            "            \"Student1\",\n" +
            "            \"Student2\"\n" +
            "        ],\n" +
            "        \"qualify\": true,\n" +
            "        \"annualFee\": 3.00\n" +
            "       }\n" +
            "]";

    JSONArray arr = new JSONArray(res);
    for (int i = 0; i < arr.length(); i++) {
        JSONObject block = arr.getJSONObject(i);
        Integer cls = block.getInt("Class");
        System.out.println("cls = " + cls);
        Object school = block.getString("school");
        System.out.println("school = " + school);
        JSONArray students = block.getJSONArray("student");
        System.out.println("student[0] = " + students.get(0));
        System.out.println("student[1] = " + students.get(1));
    }

应输出

cls = 1
school = test
student[0] = Student1
student[1] = Student2

答案 1 :(得分:4)

您的 JSON 响应根是 array ,但是您将JSON响应视为 JSON对象

如下更改您的解析json代码

String res=cspResponse.prettyPrint();
    org.json.JSONArray arr = new org.json.JSONArray(res);
    String dataStatus=null;
    for (int i = 0; i < arr.length(); i++) {
        org.json.JSONObject obj=arr.getJSONObject(i);
        dataStatus = obj.getString(key);
        System.out.println("dataStatus is \t" + dataStatus);
        String schoolName = org.getString("school");
        System.out.println("school => " + schoolName);
        org.json.JSONArray students = obj.getJSONArray("student");
        System.out.println("student[0] = " + students.get(0));
        System.out.println("student[1] = " + students.get(1));
    }

答案 2 :(得分:2)

您的JSON响应不正确。您的根是一个数组,而不是json对象

使用JSON Array类而不是JSON对象

String res=cspResponse.prettyPrint();
    org.json.JSONArray arr = new org.json.JSONArray(res);

/* rest of your parsing*/

答案 3 :(得分:1)

    You can use simple JSONObject class and Simple JSONParser for parsing the JSON.

     1. Parse the JSON. 

          org.json.simple.JSONParser parser = new org.json.simple.JSONParser();
          org.json.simple.JSONObject parsedJSON = parser.parse(inputJSON);

     2. To get class: 

          String class = parsedJSON.get("Class");

     3. To get Students: 

          org.json.simple.JSONArray studentArray = parsedJSON.get("student");

    4. To Get School: 

         String school = parsedJSON.get("school");

    After the above steps, you can run a for-loop to print the class and students.