android - I am trying to get Json Object into my mobile app, and parse it to be added in Map -
here json object
{ "shops": [ { "shop_id": "916tcr", "lat": "10.512573", "long": "76.255868", "address": "******" }, { "shop_id": "rktcr", "lat": "10.527642", "long": "76.214435", "address": "sanfrncisco,usa" }, { "shop_id": "lstcr", "lat": "10.527642", "long": "76.214435", "address": "afgfagra" }, { "shop_id": "wbstcr", "lat": "10.527642", "long": "76.214435", "address": "agkangj" }, { "shop_id": "bhttcr", "lat": "10.226967", "long": "76.193833", "address": "gjognje" }, { "shop_id": "kfctcr", "lat": "10.527642", "long": "76.214435", "address": "aijaogv" }, { "shop_id": "mctcr", "lat": "10.505201", "long": "76.269635", "address": "plmqntonf" }, { "shop_id": "bhbtcr", "lat": "10.527642", "long": "76.214435", "address": "agkbajgoj" }, { "shop_id": "dmstcr", "lat": "10.528698", "long": "76.201991", "address": "fajbjab" }, { "shop_id": "ckgtcr", "lat": "10.268945", "long": "76.157043", "address": "ajnrgj" } ] }
i want shops[0],shops[1]..... , thereby getting shop_id,lat,long...
i doing using volley library.
java code
arrayadapter<string> adapter; arraylist<string> items; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview listview=(listview)findviewbyid(r.id.listv); items=new arraylist<string>(); adapter=new arrayadapter(this, r.layout.item_layout,r.id.txt,items); listview.setadapter(adapter); } string url ="returns json file" jsonarrayrequest jsonarrayrequest=new jsonarrayrequest(url,new response.listener<jsonarray>() { public void onresponse(jsonarray jsonarray){ (int i=0;i<jsonarray.length();i++) { try { jsonobject jsonobject = jsonarray.getjsonobject(i); items.add(jsonobject.getstring("shop_id")); items.add(jsonobject.getstring("address")); items.add(jsonobject.getstring("lat")); items.add(jsonobject.getstring("long")); } catch (jsonexception e) { e.printstacktrace(); } adapter.notifydatasetchanged(); } } },new response.errorlistener() { @override public void onerrorresponse(volleyerror volleyerror){ log.e("error", "unable parse json array"); } }); requestqueue.add(jsonarrayrequest); }
`
what you're getting json object containing array named "shops". objects in shops array, first need convert response jsonobject (i called responseobject in example), shops array (shopsarray), loop through each object (shopobject) , names/values:
jsonarray shopsarray = responseobject.getjsonarray("shops"); (int = 0, < shopsarray.length(); i++) { jsonobject shopobject = shopsarray.getjsonobject(i); string shopid = shopobject.getstring("shop_id"); string latitude = shopobject.getstring("lat"); string longitude = shopobject.getstring("long"); string address = shopobject.getstring("address"); }
Comments
Post a Comment