47

I have some JSON with the following structure:

{"source":[
           {"name":"john","age":20},
           {"name":"michael","age":25},
           {"name":"sara", "age":23}
         ]
}

I have named this JSON string as mainJSON. I'm trying to access the elements "name" and "age" with the following Java code:

JSONArray jsonMainArr = new JSONArray(mainJSON.getJSONArray("source"));
for (int i = 0; i < jsonMainArr.length(); i++) {  // **line 2**
     JSONObject childJSONObject = jsonMainArr.getJSONObject(i);
     String name = childJSONObject.getString("name");
     int age     = childJSONObject.getInt("age");
}

I'm being shown the following exception for line number 2:

org.json.JSONException: JSONArray initial value should be a string or collection or array.

Please guide me as to where I'm making the mistake and how to rectify this.

1
  • There is no issue on your source code, I tried it already. Commented Dec 25, 2016 at 12:50

5 Answers 5

52

mainJSON.getJSONArray("source") returns a JSONArray, hence you can remove the new JSONArray.

The JSONArray contructor with an object parameter expects it to be a Collection or Array (not JSONArray)

Try this:

JSONArray jsonMainArr = mainJSON.getJSONArray("source"); 
2
  • 3
    How can we parse JSON like this? [{"id":"1","name":"ABC"},{"id":"2","name":"XYZ"}] Commented Apr 7, 2015 at 11:31
  • This may Help json = "{\"m\"="+json+"}"; and now parse by name 'm'. Commented Jul 13, 2015 at 19:52
10

Your code is fine, just replace the following line:

JSONArray jsonMainArr = new JSONArray(mainJSON.getJSONArray("source"));

with this line:

JSONArray jsonMainArr = mainJSON.getJSONArray("source");
2
  • What if the data format received is [{dict1},{dict2},{dict3}]? Commented Feb 6, 2014 at 0:24
  • JSONArray list = new JSONArray(jsonString); Commented Feb 12, 2014 at 14:29
0

line 2 should be

for (int i = 0; i < jsonMainArr.size(); i++) {  // **line 2**

For line 3, I'm having to do

    JSONObject childJSONObject = (JSONObject) new JSONParser().parse(jsonMainArr.get(i).toString());
0
private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
      sb.append((char) cp);
    }
    return sb.toString();
  }

 String jsonText = readAll(inputofyourjsonstream);
 JSONObject json = new JSONObject(jsonText);
 JSONArray arr = json.getJSONArray("sources");

Your arr would looks like: [ { "id":1001, "name":"jhon" }, { "id":1002, "name":"jhon" } ] You can use:

arr.getJSONObject(index)

to get the objects inside of the array.

-3

This could be an answer to your question:

JSONArray msg1 = (JSONArray) json.get("source");
for(int i = 0; i < msg1.length(); i++){
  String name = msg1.getString("name");
  int age     = msg1.getInt("age");
}
3
  • Could you please elaborate more your answer adding a little more description about the solution you provide? Commented Jun 15, 2015 at 7:05
  • doesn't work - no reference back to "i" when iterating Commented Jul 3, 2015 at 22:19
  • Cannot cast the string to JSONArray, this doesn't work Commented Dec 25, 2016 at 12:52

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.