Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在json数组中添加json对象,在sqlite的json对象中添加AGIA json数组_Java_Android_Arrays_Json - Fatal编程技术网

Java 在json数组中添加json对象,在sqlite的json对象中添加AGIA json数组

Java 在json数组中添加json对象,在sqlite的json对象中添加AGIA json数组,java,android,arrays,json,Java,Android,Arrays,Json,我是android新手,我想获得如下格式的json 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.

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

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

这是代码

 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;
    }
请尝试以下代码:

 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;
    }
移动到do内部的行上方,同时像下面一样循环

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)
{
}
移动到do内部的行上方,同时像下面一样循环

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

那你需要什么?你得到什么和想要什么没有区别这是一项家庭任务??祝贺你!你已经得到了你想要的结果:)就在第二秒钟,我发布了相同的图片,让我编辑我得到的图片。我看不出两张图片有什么不同。你需要什么?你得到什么和想要什么没有区别这是一项家庭任务??祝贺你!你已经得到了你想要的结果:)就在我发布同一张图片的第二秒钟,让我编辑我得到的图片。我看不出两张图片有任何区别。谢谢,效果很好。这正是我想要的。谢谢,它工作得很好。这正是我想要的