How to access nested arrays in a JSON object (Android) -
the structure of json is,
"posts":[ //post 1. { "images":{ "small":{ "url": "http://..." "width": 64 }, "large":{ "url": "http://..." "width": 128 } }, "caption":"..." }, { //post 2 "images":{ "small":{ "url": "http://..." "width": 64 }, "large":{ "url": "http://..." "width": 128 } }, "caption":"..." } ] i want get, posts -> images -> large -> url each post in form of string.
here's i've done:
jsonarray jsonarray = jsonobject.getjsonarray("posts"); (int = 0; < jsonarray.length(); i++) { jsonobject jsonobject = jsonarray.getjsonobject(i); jsonarray innerarray1 = jsonobject.getjsonarray("images"); jsonarray innerarray2 = innerarray1.getjsonarray(0); //gets "large" jsonarray innerarray3 = innerarray2.getjsonarray(1); //gets "url" string finalstring = innerarray3.tostring(); } i expected value of "url" in finalstring, except ends being empty.
what doing wrong?
"images", "small", "large" jsonobjects(notice curly braces), not jsonarrays(square brackets) , need extract them such.
jsonarray jsonarray = jsonobject.getjsonarray("posts"); (int = 0; < jsonarray.length(); i++) { jsonobject jsonobject = jsonarray.getjsonobject(i); jsonobject innerobject1 = jsonobject.getjsonobject("images"); jsonobject innerobject2 = innerobject1.getjsonobject("large"); //gets "large" string finalstring = innerobject2.getstring("url"); }
Comments
Post a Comment