java - JSONArray Parsing error -
i need parsing json array.
[ { "header1": [ { "path": "upload/images/1430572021716.jpg" }, { "path": "upload/images/1430574003703.jpg" } ] }, { "header2": [ { "path": "upload/images/1430574124119.jpg" }, { "path": "upload/images/1430574203001.jpg" } ] } ]
i receiving above jsonarray perfectly. want iterate through array , extract both header text "header1" , value of path
i keep running following error message
at 0 of type org.json.jsonarray cannot converted jsonobject
after research, due system not being able parse json array. work if change "list" array objct, not array , loose ability iterate through it.
here code tried parse array with
jsonarray marray; marray = json; //download json array (int = 0; < marray.length(); i++) { if(marray != null) { jsonarray list = marray.getjsonarray(i); if(list != null) { for(int = 0; < list.length();a++) { jsonobject elem = list.getjsonobject(a); if (elem != null) { listdata.add(elem.getstring("path")); } } } } }
you're trying treat each element of top-level array array - it's not, it's object. object has field value is array. want like:
for (int = 0; < json.length(); i++) { jsonobject container = json.getjsonobject(i); // don't know property name, there's one, apparently... string key = container.keys().next(); jsonarray subarray = container.getjsonarray(key); (int j = 0; j < subarray.length(); j++) { listdata.add(subarray.getjsonobject(j).getstring("path")); } }
Comments
Post a Comment