android - Json array and object inside json object -
this question has answer here:
- how parse json in android 4 answers
{ "status": 200, "message": "api executed successfully.", "data": [{ "data": [{ "shoptoken": "nq2", "id": 5, "userid": 5, "shopname": "test", "contactname": "test", "contactnumber": "test", "address": null, "isactive": true, "ownername": "test" }], "recordsfiltered": 1, "recordstotal": 1, "draw": 0, "pageindex": 0, "pagesize": 1 }] }
how can parse json in android?
@override protected string doinbackground(void... voids) { okhttpclient client = new okhttpclient.builder().connecttimeout(5, timeunit.minutes) .readtimeout(5, timeunit.minutes) .build(); request request = new request.builder() .url(config.gloposnet_url+"getshops?usertoken=nq2") .build(); try { response response = client.newcall(request).execute(); string res = response.body().string(); try { jsonobject joa = new jsonobject(res); int status = joa.getint("status"); msg = joa.getstring("message"); system.out.println("status : "+status); if (status == 200){ jsonarray infoa = joa.getjsonarray("data"); (int i=0; i<=infoa.length(); i++){ jsonobject johaa = infoa.getjsonobject(i); system.out.println("object=== "+johaa.tostring()); modelclassshoplist pstshoplist = new modelclassshoplist(); pstshoplist.getshoptoken(); pstshoplist.getid(johaa.getint("id")); pstshoplist.getuserid(johaa.getstring("userid")); pstshoplist.getshopname(johaa.getstring("shopname")); pstshoplist.getcontactname(johaa.getstring("contactname")); pstshoplist.getcontactnumber(johaa.getstring("contactnumber")); pstshoplist.getaddress(johaa.getstring("address")); pstshoplist.getownername(johaa.getstring("ownername")); string shop_token = pstshoplist.getshoptoken(); string user_name = pstshoplist.getshopname(johaa.getstring("userid")); string user_type = pstshoplist.getcontactname(johaa.getstring("userid")); string user_addres = pstshoplist.getcontactnumber(johaa.getstring("userid")); system.out.println("shop token=== "+shop_token); sharedpreferences pref = getapplicationcontext().getsharedpreferences("mypref", mode_private); sharedpreferences.editor editor = pref.edit(); editor.putstring(config.shop_token, shop_token); editor.commit(); } }else { alertdialog.builder builder; if (build.version.sdk_int >= build.version_codes.lollipop) { builder = new alertdialog.builder(mainactivity.this, android.r.style.theme_material_light_dialog_alert); builder.setmessage(""+msg); builder.setcancelable(false); builder.setpositivebutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } } catch (jsonexception e) { e.printstacktrace(); } return res; } catch (ioexception e) { e.printstacktrace(); } return null; }
to data array json string
jsonobject jobject = new jsonobject(youstringdata); jsonarray jarray = jobject.getjsonarray("data");
to data inside first 1 data
for(int = 0; < jarray.length(); i++){ jsonarray datatwo = jarray.getjsonobject(i).getjsonarray("data");; }
if want other value in main object can use
jobject.getint("status")
Comments
Post a Comment