【问题标题】:adding json object in json array and agian json array in json object from sqlite在 json 数组中添加 json 对象,在 sqlite 的 json 对象中添加 agian json 数组
【发布时间】:2017-04-06 06:33:44
【问题描述】:

我是 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

【问题讨论】:

  • 那么你需要什么?你得到什么和你想要什么没有区别
  • 这是一个家庭任务??
  • 恭喜!你得到了你想要的结果:)
  • 就在我发布同一张图片的第二秒让我编辑我得到的内容
  • 我看不出两张图片有什么不同

标签: java android arrays json


【解决方案1】:
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)
{
}

【讨论】:

    【解决方案2】:

    试试下面的代码:

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

    【讨论】:

    • 感谢它运行良好。它的工作正是我想要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    相关资源
    最近更新 更多