I m new to android i want to get json
format like below
here is image what i want json output
here is code
public JSONObject getjsondataofcart(){
JSONObject modelList = new JSONObject();
JSONArray myara = new JSONArray();
String query = "select * from "+ cart_table;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query,null);
JSONObject data = new JSONObject();
try {
if (cursor.moveToFirst()) {
do {
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 as below:
any help will be appreciated. thanks in advanced
Try with below code:
public JSONObject getjsondataofcart(){
JSONObject modelList = new JSONObject();
JSONArray myara = new JSONArray();
String query = "select * from "+ cart_table;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query,null);
try {
if (cursor.moveToFirst()) {
do {
//Add this line here
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)
{
}
return modelList;
}
JSONObject data = new JSONObject();
Move above line inside of do-while loop like below
try {
if (cursor.moveToFirst()) {
do {
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)
{
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.