java - Error when getting data from JSON Nested loop in Android -
i tried getting error in loop optjsonobject(i)
{ "response": { "code": 1, "message": "success" }, "data": { "updates": [ { "id":"67", "date":"6 months ago", "update_type": "7", "update_id": "67", "name":"ravi" }, { "id":"68", "date":"3 months ago", "update_type": "5", "update_id": "68", "name":"paresh" }, { "id":"69", "date":"1 months ago", "update_type": "6", "update_id": "69", "name":"sampath" }, { "id":"62", "date":"9 months ago", "update_type": "6", "update_id": "62", "name":"raju" } ] } }
the code tried :
try { inputstream = null; = getapplicationcontext().getresources().openrawresource(r.raw.myfile); //converts stream obj in string obj mjsonstring = convertstreamtostring(is); jsonobject obj = new jsonobject(mjsonstring); jsonobject dataobj = obj.getjsonobject("data"); jsonobject updateobj = dataobj.getjsonobject("update"); //system.out.println(new jsonobject(update)tostring(2)); for(int i=0; i<updateobj.length(); i++){ jsonobject object = updateobj.optjsonobject(i); mupdates = new mobile_updateactivity(); mupdates.update_id = object.getstring("update_id"); mupdates.site_id = object.getstring("site_id"); mupdates.update_type = object.getstring("update_type"); system.out.println(mupdates.update_id); system.out.println(mupdates.site_id); system.out.println(mupdates.update_type); }
}
but i'm facing error @ **optjsonobject(i)
error: method optjsonobject(string) in type jsonobject not applicable arguments (int)**
you getting following warning :
optjsonobject(i) error : method optjsonobject(string) in type jsonobject not applicable arguments (int)
because in current json string updates
jsonarray
instead of jsonobject
trying convert jsonarray jsonobject.you calling jsonobject.optjsonobject
jsonobject require string parameter instead of jsonarray.optjsonobject
require index of item in current jsonarray. change code avoid current warning :
//...your code here... jsonobject obj = new jsonobject(mjsonstring); jsonobject dataobj = obj.getjsonobject("data"); // update jsonarray dataobj jsonobject jsonarray updateobj = dataobj.getjsonarray("update"); //...your code here...
Comments
Post a Comment