[英]adding json object in json array and agian json array in json object from sqlite
我是android的新手我希望得到如下的json
格式
這是代碼
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;
}
我拿出如下:
任何幫助將不勝感激。 提前致謝
試試以下代碼:
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();
如下所示在do-while循環內移動線
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)
{
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.