1

Unable to set array inside JSONObject according to the response.Below is my code in which I am unable to set array in jsonobject. How to send key value for array inside my jsonobject for which shared the response which code is getting from postman

Is this the right way in code

Code--

     JsonArray array = new JsonArray();
            array.add(productId);
            array.add(qty);
     JSONObject jsonObject = new JSONObject();
                jsonObject.put("productDetails", array);**

This is the code in MainActivity. The problem is not getting correct jsonarray in my JSON object so API will not hit correctly These String key values are used to pass in request params

    String key="WSEoaGBifOEIS5dd6vQ5tfbs3R1c8Rsz";
            String affId="teamfotog";
            String act="photoStores";
            String latitude="40.7127753";
            String longitude="-74.0059728";
            String devinf="Android,7.0";
            String appver="1.00";
            String productId="6670002";
            String qty="3";
            //productDetails
            **JsonArray array = new JsonArray();
            array.add(productId);
            array.add(qty);**

            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("apiKey", key);
                jsonObject.put("affId", affId);
                jsonObject.put("act", act);
                jsonObject.put("latitude", latitude);
                jsonObject.put("longitude", longitude);
                jsonObject.put("devinf", devinf);
                jsonObject.put("appver", appver);
                **jsonObject.put("productDetails", array);**

    JsonParser jsonParser = new JsonParser();

    ApiStorePhotoInterface apiInterface = ApiStorePhotoClient.getApi();

    Call<PhotoStoreMainModel> call = apiInterface.getResponse((JsonObject) jsonParser.parse(jsonObject.toString().trim()));

Request Params is in Jsonbody --

{"apiKey":"WSEoaGBifOEIS5dd6vQ5tfbs3R1c8Rsz","affId":"teamfotog","act":"photoStores","latitude":"40.7127753","longitude":"-74.0059728","devinf":"Android,7.0","appver":"1.00","productDetails":[{"productId":"6670002","qty":"3"}]}

4 Answers 4

1

Of course, it won't work. You are directly adding objects(Strings) in your JsonArray. In the response body, what you really want is a JsonObject inside the JsonArray. Try this -

JsonObject productDetail = new JsonObject();
productDetail.addProperty("productId", productId);
productDetail.addProperty("qty", qty);

JsonArray array = new JsonArray();
array.add(productDetail);
Sign up to request clarification or add additional context in comments.

11 Comments

So I have to create new jsonobject for this ?
JSONArray is not working but JsonArray is working with null response
From response body, you can see that it's an array of objects.
{ "apiKey": "WSEoaGBifOEIS5dd6vQ5tfbs3R1c8Rsz", "affId": "teamfotog", "act": "photoStores", "latitude": "40.7127753", "longitude": "-74.0059728", "devinf": "Android,7.0", "appver": "1.00", "productDetails": "[\"{\\\"productId\\\":\\\"6670002\\\",\\\"qty\\\":\\\"3\\\"}\"]" } getting this from debugging
|
1

Try this .

jsonObject.put("productDetails",(Object)array);

3 Comments

getting this request from debbuger ---
{ "apiKey": "WSEoaGBifOEIS5dd6vQ5tfbs3R1c8Rsz", "affId": "teamfotog", "act": "photoStores", "latitude": "40.7127753", "longitude": "-74.0059728", "devinf": "Android,7.0", "appver": "1.00", "productDetails": "[\"{\\\"productId\\\":\\\"6670002\\\",\\\"qty\\\":\\\"3\\\"}\"]" }
Expected --{ "apiKey": "WSEoaGBifOEIS5dd6vQ5tfbs3R1c8Rsz", "affId": "teamfotog", "act": "photoStores", "latitude": "40.7127753", "longitude": "-74.0059728", "devinf": "Android,7.0", "appver": "1.00", "productDetails": [{ "productId": "6670002", "qty": "3" }] }
1

If someone is still having problems with this, this is what fixed it for me:

Use:

JSONArray

istead of

JsonArray

Comments

0

You should put product detail model to array

            String key="WSEoaGBifOEIS5dd6vQ5tfbs3R1c8Rsz";
            String affId="teamfotog";
            String act="photoStores";
            String latitude="40.7127753";
            String longitude="-74.0059728";
            String devinf="Android,7.0";
            String appver="1.00";
            String productId="6670002";
            String qty="3";     

            JsonObject product = new JsonObject();
            product.put("productId",productId);
            product.put("qty",qty);


            JsonArray array = new JsonArray();
            array.add(product);


            JSONObject jsonObject = new JSONObject();
            jsonObject.put("apiKey", key);
            jsonObject.put("affId", affId);
            jsonObject.put("act", act);
            jsonObject.put("latitude", latitude);
            jsonObject.put("longitude", longitude);
            jsonObject.put("devinf", devinf);
            jsonObject.put("appver", appver);
            jsonObject.put("productDetails", array);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.