1

I have two items in arraylist

  • Position 0 : product_id=500,amout=1
  • Position 1 : product_id=501,amout=1

I want to create this type of json

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

This is my code

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

I am getting this Responce:

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

}

But I want this json Responce please help for getting resultant responce. { "user_id": "3", "shipping_id": "1", "payment_id": "2", "products": { "500": { "product_id": "500", "amount": "1" }, "501": { "product_id": "501", "amount":"1" } } }

2 Answers 2

2

You can use the following code to generate your 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();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks sir@Muthukrishnan Rajendran
1

try this one..

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

1 Comment

"products" is JsonObject, not JsonArray

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.