java - adding json object in json array and agian json array in json object from sqlite -


i m new android want json format below

here image want json output

here code

 public jsonobject getjsondataofcart(){     jsonobject modellist = new jsonobject();     jsonarray myara   = new jsonarray();     string query = "select * "+ cart_table;     sqlitedatabase db = this.getwritabledatabase();     cursor cursor = db.rawquery(query,null);     jsonobject data = new jsonobject();     try {        if (cursor.movetofirst()) {         {            data.put("id", cursor.getint(1));           data.put("qty", cursor.getint(2));           data.put("amount", cursor.getint(7));           data.put("tax_id","0");           data.put("sub_total", cursor.getint(6));           myara.put(data);         } while (cursor.movetonext());          modellist.put("product_detail",myara);       }     }     catch (jsonexception e)     {     }      return modellist;   } 

i got out put below:

here got

any appreciated. in advanced

jsonobject data = new jsonobject();  

move above line inside of do-while loop below

try {    if (cursor.movetofirst()) {     {       jsonobject data = new jsonobject();       data.put("id", cursor.getint(1));       data.put("qty", cursor.getint(2));       data.put("amount", cursor.getint(7));       data.put("tax_id","0");       data.put("sub_total", cursor.getint(6));       myara.put(data);     } while (cursor.movetonext());      modellist.put("product_detail",myara);   } } catch (jsonexception e) { } 

Comments