java - How to parse multilevel json data in android -
i have multilevel json data , want parse json array.but throw exception:
> -e/error jsonexception: value [{"name":"desktop","children_lv3":[{"name":"dell > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_60_62"},{"name":"hcl > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_60_63"},{"name":"hp > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_60_64"},{"name":"ibm > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_60_65"},{"name":"lenevo > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_60_66"}],"href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_60"},{"name":"laptop","children_lv3":[{"name":"acer > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_68_69"},{"name":"hcl > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_68_70"},{"name":"hp > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_68_71"},{"name":"ibm > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_68_72"}],"href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_68"},{"name":"monitor","children_lv3":[{"name":"dell > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_73_74"},{"name":"lg > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_73_75"},{"name":"samsung > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_73_76"},{"name":"soni > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_73_77"},{"name":"wipro > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_73_78"}],"href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_73"},{"name":"laptop > accessories","children_lv3":[{"name":"accessories > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_79_80"},{"name":"dvd > design > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_79_81"},{"name":"hdd > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_79_82"},{"name":"keyboard > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_79_83"},{"name":"mouse > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_79_84"}],"href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_79"},{"name":"desktop > accessories","children_lv3":[{"name":"accessories > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_86_87"},{"name":"external > accessories > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_86_88"},{"name":"hdd > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_86_89"},{"name":"keyboard > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_86_90"},{"name":"mouse > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_86_91"},{"name":"software > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_86_92"}],"href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_86"},{"name":"printer > , scanner","children_lv3":[{"name":"epson > (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_93
my mainactivity.java:-
public class mainactivity extends appcompatactivity implements fragmentdrawer.fragmentdrawerlistener { //private static string tag = mainactivity.class.getsimplename(); private toolbar mtoolbar; private fragmentdrawer drawerfragment; string data1,data2,data3,data4,data5; int cnt = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mtoolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(mtoolbar); getsupportactionbar().setdisplayshowhomeenabled(true); drawerfragment = (fragmentdrawer) getsupportfragmentmanager().findfragmentbyid(r.id.fragment_navigation_drawer); drawerfragment.setup(r.id.fragment_navigation_drawer, (drawerlayout) findviewbyid(r.id.drawer_layout), mtoolbar); drawerfragment.setdrawerlistener(this); // display first navigation drawer view on app launch displayview(0); log.d("oncreate method",""); new productsasyntask().execute("http://opencart.codeniques.com/shopping/?route=feed/web_api/menu&key=test123$"); } public class productsasyntask extends asynctask<string,void,void>{ progressdialog dialog; protected void onpreexecute(){ super.onpreexecute(); log.d("in onpreexceute",""); dialog = new progressdialog(mainactivity.this); dialog.setmessage("loading, please wait"); dialog.settitle("connecting server"); dialog.show(); dialog.setcancelable(false); } protected void doinbackground(string... param){ try{ log.d("in doinbackground",""); httpclient client= new defaulthttpclient(); httppost post = new httppost(param[0]); httpresponse response = client.execute(post); int status = response.getstatusline().getstatuscode(); if(status == 200){ log.d("status",""+status); httpentity entity = response.getentity(); string data = entityutils.tostring(entity); jsonobject jsonobject = new jsonobject(data); jsonarray jarray = jsonobject.getjsonarray("categories"); for(int = 0;i < jarray.length();i++){ cnt++; log.d("value of array",jarray.length()+""); log.d("value of i",""+i); jsonobject jsonobject1 = jarray.getjsonobject(i); data1 = jsonobject1.getstring("name"); log.d("hello ",data1); jsonobject jsonobject2 = jsonobject1.getjsonobject("children"); for(int j=0;j<jsonobject2.length();j++){ data2 = jsonobject2.getstring("name"); log.d("hello2",data2); jsonobject jsonobject3 = jsonobject2.getjsonobject("children_lv3"); for(int k=0;k<jsonobject3.length();k++){ data3 = jsonobject3.getstring("name"); log.d("hello3 ",data3); data4 = jsonobject3.getstring("href"); log.d("hello4 ",data4); } data5=jsonobject2.getstring("href"); log.d("hello5 ",data5); } } } }catch(ioexception e){ log.e("error ioexception :",e.getmessage()); }catch (jsonexception e){ log.e("error jsonexception",e.getmessage()); } return null; } protected void onpostexecute(void avoid) { super.onpostexecute(avoid); dialog.dismiss(); log.d("counter value",""+cnt); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } if(id == r.id.action_search){ toast.maketext(getapplicationcontext(), "search action selected!", toast.length_short).show(); return true; } return super.onoptionsitemselected(item); } @override public void ondraweritemselected(view view, int position) { displayview(position); } private void displayview(int position) { fragment fragment = null; string title = getstring(r.string.app_name); switch (position) { case 0: fragment = new homefragment(); title = getstring(r.string.title_home); break; case 1: fragment = new friendsfragment(); title = getstring(r.string.title_friends); break; case 2: fragment = new messagesfragment(); title = getstring(r.string.title_messages); break; default: break; } if (fragment != null) { fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.replace(r.id.container_body, fragment); fragmenttransaction.commit(); // set toolbar title getsupportactionbar().settitle(title); } } }
first, use http://jsonlint.com/ validate json parsing.
once observe "categories" "children" json array , not object. so, need use
jsonobject1.getjsonarray("children");
hope helps
Comments
Post a Comment