1

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.

1
  • yet same. So I have updated question with update-1 with stack trace. Commented Oct 1, 2019 at 11:52

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

6 Comments

I am getting error when, its assigned res with json array.
Fix you json source first - the last comma in < "annualFee": 3.00,> phrase make the parsing fail
I fixed it, yet same.
Copy the comment's code and paste it to become your main method's body. It is compilable, runnable and produce the output listed here. If then you have an error - post more details here.
yet same. So I have updated question with update-1 with stack trace.
|
1

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

2 Comments

It says org.json.JSONException: JSONObject["student"] not a string.
yet same. So I have updated question with update-1 with stack trace.
0
    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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.