1

I have this json:

[{ "name": "prog 1", "show": [{ "name": "n1", "time": "01.10 " }, { "name": "n2", "time": "01.35 " }] }, { "name": "prog 2", "show": [{ "name": "n1", "time": "01.10 " }, { "name": "n2", "time": "01.35 " }] }] 

Now trying to parse it in Java like:

JSONObject json=new JSONObject(json_str); 

throws an Exception, since it doesn't begin with {, but [ since it's an array. I can parse this without problem in js, but aparently I cannot load an JSONArray with this string...

4
  • check this -> stackoverflow.com/questions/5650171/… if it can help. Commented Sep 10, 2014 at 12:30
  • 1
    Sometimes it gets easier for us to help you if you not only describe the exception you got, but also include the stacktrace in your question. Commented Sep 10, 2014 at 12:31
  • 1
    The input contains an array, not an object. Commented Sep 10, 2014 at 12:35
  • Go to json.org and study the syntax. You have an array of objects (and the objects contain arrays). You need to use JSONArray. Commented Sep 10, 2014 at 12:39

3 Answers 3

1

use: JSONArray objArray = new JSONArray (json_str);

// to access the individual objects inside the array: for(int i=0;i<objArray.length();i++) { JSONObject obj = objArray.getJSONObject(i); } 
Sign up to request clarification or add additional context in comments.

Comments

1

Have you tried this:

 JSONArray arr = new JSONArray(stringWithContent); 

Then access it like :

 for(int i = 0; i<arr.length();i++){ System.out.println(arr.get(i)); } 

Comments

0

You can try following code

JSONObject jObject = new JSONObject(json_str); JSONArray array = jObject.getJSONArray("show"); for(int i = 0 ; i < array.length() ; i++) { System.out.println(array.getJSONObject(i).getString("name")); System.out.println(array.getJSONObject(i).getString("time")); } 

It will helpful ...

Comments