-2

I'm working on an android project where I have to show some items in response of a request I'll send to server using provided API.

I'll get an JSON object in response which contains a JSON array as one of the elements inside of it.

My problem is the JSON array may have several JSON objects inside of it and how to handle this situation is beyond my knowledge.

"Details": [
    {
    "OrderId": 7615,
    "ProductId": 1292406,
    "ProductImageFormat": "jpg",
    "ProductImageId": 1752,
    "ProductImageUrl": "",
    "ProductOrgImageUrl": "",
    "ProductLargeImageUrl": "",
    "ProductName":""
    "ProductSlug": ""
    "ProductSubTitle": ""
    "Quantity": 1,
    "UnitPrice": 8300
    },
    {
    "OrderId": 7615,
    "ProductId": 23568452,
    "ProductImageFormat": "jpg",
    "ProductImageId": 1895,
    "ProductImageUrl": "",
    "ProductOrgImageUrl": "",
    "ProductLargeImageUrl": "",
    "ProductName":""
    "ProductSlug": ""
    "ProductSubTitle": ""
    "Quantity": 1,
    "UnitPrice": 3500
    }
]

I may get several of this single object with different values in one Array as you can see above.

Is there anything I can do about this?

EDIT: what I mean is that the array size is dynamic and I don't know how to handle dynamic sized JSON array

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
MrSinaRJ
  • 57
  • 13
  • Please do a little more research on your own. On the internet there are a many examples of how to parse json with `JSONObject` and `JSONArray`. – Barns Oct 26 '17 at 17:55
  • I think it's safe to say I searched and when I couldn't find anything I post my case here! I didn't post everything for a exact answer, I just wanted help or a guide to a correct path of documents regarding my issue – MrSinaRJ Oct 26 '17 at 17:58
  • Based on your json example you posted and the lack of any code in your post I think it is safe to say you did very little research. – Barns Oct 26 '17 at 18:04
  • Possible duplicate of [How to parse JSON](https://stackoverflow.com/questions/2591098/how-to-parse-json) – Barns Oct 26 '17 at 18:10
  • Not a duplicate of what you have posted here, and that JSON is from my server, I had to delete some information! so It's better to be helpful and not be like this under a post with a valid answer! Thanks for your time and your lack of concentrate to see what exactly my post is – MrSinaRJ Oct 26 '17 at 18:14

1 Answers1

0

first of all, the JSON you provided is invalid. So I'm going to correct it and assume the following (the "Details" array is wrapped in a JSONObject)

{
"Details": [
    {
    "OrderId": 7615,
    "ProductId": 1292406,
    "ProductImageFormat": "jpg",
    "ProductImageId": 1752,
    "ProductImageUrl": "",
    "ProductOrgImageUrl": "",
    "ProductLargeImageUrl": "",
    "ProductName":"",
    "ProductSlug": "",
    "ProductSubTitle": "",
    "Quantity": 1,
    "UnitPrice": 8300
    },
    {
    "OrderId": 7615,
    "ProductId": 23568452,
    "ProductImageFormat": "jpg",
    "ProductImageId": 1895,
    "ProductImageUrl": "",
    "ProductOrgImageUrl": "",
    "ProductLargeImageUrl": "",
    "ProductName":"",
    "ProductSlug": "",
    "ProductSubTitle": "",
    "Quantity": 1,
    "UnitPrice": 3500
    }
   ]
}

Get the array and iterate it (assuming the outer object is called jo):

  try {
        JSONArray ja = jo.getJSONArray("Details");
        for (int i = 0; i < ja.length(); i++) {
            JSONObject jsonObject = ja.getJSONObject(i);
            Log.i("JSON", jsonObject.toString());
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

This way you can handle JSON arrays of arbitrary length.

Droidman
  • 11,485
  • 17
  • 93
  • 141
  • Yes, I just posted the inner JSON array thanks for your answer, so I can interact with json array like any other array and iteration is a good choice That was a lot of help and just cleared everything for me – MrSinaRJ Oct 26 '17 at 18:04
  • @MrSinaRJ your inner objects were also missing several commas – Droidman Oct 26 '17 at 18:05
  • sry, that was a poor copy paste from my response Your answer was perfect for me – MrSinaRJ Oct 26 '17 at 18:06