Sobhit Sharma
Sobhit Sharma

Reputation: 713

Parse JSON multiple objects

I am trying to parse the below json but unable to do that as stack over flow error comes in.

Here is the JSON -

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

Here is the code which is failing currently.

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

Usecases are:

  1. To get the value key "class"
  2. Get the value from Student
  3. Get the value from school

I appreciate your help.

update-1 Code more info on stack trace updated with below details. cls = 1

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

Stack trace-

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

When I ran the code with the below answers, here its failing for student is not a string.

The answers I used from first two comments and both have the same error. I appropriate your help.

Upvotes: 1

Views: 5554

Answers (3)

Alireza Bideli
Alireza Bideli

Reputation: 874

Your JSON reponse root is array but you consider your JSON response as JSON object

Changing your parsing json code as below

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

Upvotes: 1

Ganesh A
Ganesh A

Reputation: 16

    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.

Upvotes: 0

lotor
lotor

Reputation: 1090

Your json fragment is invalid - the last comma breaks the parsing. But the rest of the code is quite workable.

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

should output

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

Upvotes: 2

Related Questions