1

I have JSON from file.

{  
  "from_excel":[  
    {  
      "solution":"Fisrt",
      "num":"1"
    },
    {  
      "solution":"Second",
      "num":"2"
    },
    {  
      "solution":"third",
      "num":"3"
    },
    {  
      "solution":"fourth",
      "num":"4"
    },
    {  
      "solution":"fifth",
      "num":"5"
    }
  ]
}

and want to parse list of Solution and Num.

Used lib org.json.simple.*

Try this one

        Object obj = parser.parse(new FileReader("E:\\json.txt"));

        JSONObject jsonObject = (JSONObject) obj;

        out.println(jsonObject.get("from_excel"));

        JSONObject obj_new = (JSONObject) jsonObject.get("from_excel");

        JSONArray solution = (JSONArray) obj_new.get("solution");

        Iterator iterator = solution.iterator();
        while (iterator.hasNext()) {
            out.println(iterator.next());
        }

What am I doing wrong?

If try to write

JSONObject solutions = (JSONArray) jsonObject.get("from_excel");

enter image description here

If try to write

JSONArray array = obj.getJSONArray("from_excel");

get error enter image description here

3 Answers 3

3

working solution

JSONParser parser = new JSONParser();
JSONObject jsonObject;
try {

    jsonObject = (JSONObject) parser.parse(new FileReader("E:\\json.txt"));

    out.println("<br>"+jsonObject);


    JSONArray from_excel = (JSONArray)jsonObject.get("from_excel");
    // for row output 1
    for(Object o: from_excel){
        out.println("<br>"+o);
    }
    // for row output 2
    Iterator iterator = from_excel.iterator();
    while (iterator.hasNext()) {
        out.println("<br>"+iterator.next());
    }
    // for item output 3
    for (int i = 0; i < from_excel.size(); i++) {

        JSONObject jsonObjectRow = (JSONObject) from_excel.get(i);
        String num = (String) jsonObjectRow.get("num");
        String solution = (String) jsonObjectRow.get("solution");
        out.println("<br>num="+num+"; solution="+solution);
    }
} catch (Exception e) {
    out.println("Error: "+e);
}
Sign up to request clarification or add additional context in comments.

Comments

2

There is no "solution" array in the JSON. "from_excel" is the array. So your code should look like this :

Object obj = parser.parse(new FileReader("E:\\json.txt"));

    JSONObject jsonObject = (JSONObject) obj;

    out.println(jsonObject.get("from_excel"));

    JSONArray solutions = (JSONArray) jsonObject.get("from_excel");

    Iterator iterator = solutions.iterator();
    while (iterator.hasNext()) {
        out.println(iterator.next());
    }

1 Comment

Its solution must working on lib "org.json.simple.*"? I try and get error with Incompatible types
0

from_excel is JSON array not an object. So you should achieve it is array.

JSONObject jsonObject = (JSONObject) obj;
JSONArray array = obj.getJSONArray("from_excel");

then iterate the array and get each jsonobject. Something like the below

for(int i = 0 ; i < array.length() ; i++){
    array.getJSONObject(i).getString("solution");
}

1 Comment

Its solution must working on lib "org.json.simple.*"? I try and get error about Cannot resolve method (add picture with error in body of question)

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.