簡體   English   中英

解析 JSON 多個對象

[英]Parse JSON multiple objects

我正在嘗試解析下面的 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. 從學校獲得價值

我感謝您的幫助。

update-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.");
    }

當我使用以下答案運行代碼時,這里的學生失敗不是字符串。

我從前兩條評論中使用的答案都具有相同的錯誤。 我占用了你的幫助。

您的 json 片段無效 - 最后一個逗號會中斷解析。 但是代碼的rest是相當可行的。

    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));
    }

應該 output

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

您的JSON響應根是數組,但您將 JSON 響應視為JSON ZA8CFDE6331BD59EB26ACZF86

更改您的解析 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));
    }

您的 JSON 響應不正確。 你的根是一個數組而不是一個 json object

使用 JSON 數組 class 代替 JSON Z497031794414A5524435F90151AC3

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

/* rest of your parsing*/
    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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM