gpt4 book ai didi

java - 在 json 数组中添加 json 对象,并在 sqlite 的 json 对象中添加 agian json 数组

转载 作者:太空宇宙 更新时间:2023-11-04 11:36:15 25 4
gpt4 key购买 nike

我是 android 新手,我想获得如下所示的 json 格式

here is image what i want json output

这是代码

 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;
}

我的输出如下:

here is what i got

最佳答案

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)
{
}

关于java - 在 json 数组中添加 json 对象,并在 sqlite 的 json 对象中添加 agian json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43247439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com