【问题标题】:How to create Json from Arraylist value?如何从 Arraylist 值创建 Json?
【发布时间】:2017-06-24 12:24:37
【问题描述】:

我在数组列表中有两个项目

  • 位置 0:product_id=500,amout=1
  • 位置 1:product_id=501,amout=1

我想创建这种类型的json

{
  "user_id": "3",
  "shipping_id": "1",
  "payment_id": "2",
  "products": {
      "500": {
        "product_id": "500",
        "amount": "1"
       },
      "501": {
        "product_id": "501",
        "amount":"1"
      }
  }
}

这是我的代码

JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("user_id", usr);
jsonObject.accumulate("shipping_id", shipping_id);
jsonObject.accumulate("payment_id", payment_id);

for( i=0;i<alProductss.size();i++) {
    String a = alProductss.get(i).getAnount();
    String b = alProductss.get(i).getProduct_id();
    JSONObject productObject = new JSONObject();
    jsonObject.accumulate("products", productObject);
    JSONObject numberObject = new JSONObject();
    numberObject.accumulate("product_id", b);
    numberObject.accumulate("amount", a);
    productObject.accumulate(b, numberObject);
}

我收到了这个回复:

 {
 "user_id":"230",
 "shipping_id":"1",
 "payment_id":"14",
  "products":[
     {"579":
     {
        "product_id":"579",
        "amount":"1"
        }
     },
     {"593":
        {
        "product_id":"593",
        "amount":"1"
        }
        }]

}

但我想要这个 json 响应,请帮助获得结果响应。 { "user_id": "3", "shipping_id": "1", "payment_id": "2", "products": { "500": { "product_id": "500", "amount": "1" }, "501": { "product_id": "501", "amount":"1" } } }

【问题讨论】:

    标签: android json arraylist


    【解决方案1】:

    您可以使用以下代码来生成您的 JSON,

        try {
            JSONObject jsonObject = new JSONObject();
    
            jsonObject.put("user_id", usr);
            jsonObject.put("shipping_id", shipping_id);
            jsonObject.put("payment_id", payment_id);
    
            JSONObject productValueObject = new JSONObject();
            for (int i = 0; i < alProductss.size(); i++) {
                String a = alProductss.get(i).getAmount();
                String b = alProductss.get(i).getProduct_id();
    
                JSONObject projectObj = new JSONObject();
                projectObj.put("product_id", b);
                projectObj.put("amount", a);
    
                productValueObject.put(b, projectObj);
            }
            jsonObject.put("products", productValueObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    

    【讨论】:

    • 谢谢先生@Muthukrishnan Rajendran
    【解决方案2】:

    试试这个..

      JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("user_id", usr);
        jsonObject.accumulate("shipping_id", shipping_id);
        jsonObject.accumulate("payment_id", payment_id);
    
        JSONArray jsonArray = new JSONArray();
    
        for( i=0;i<alProductss.size();i++) {
            String a = alProductss.get(i).getAnount();
            String b = alProductss.get(i).getProduct_id();
            JSONObject productObject = new JSONObject();
            JSONObject numberObject = new JSONObject();
            numberObject.accumulate("product_id", b);
            numberObject.accumulate("amount", a);
            jsonArray.put(numberObject);
        }
        jsonObject.put("products",jsonArray);
    

    【讨论】:

    • "products" 是 JsonObject,而不是 JsonArray
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 2013-04-04
    相关资源
    最近更新 更多