How to handle both JSON array and JSON object response

841 Views Asked by At

I have a JSON array which I receive from a HTTP request :

[
  '{{repeat(5, 7)}}',
  {
    _id: '{{objectId()}}'
  }
]

At times I receive only a simply JSON object and not an array.

How do I generalize and store the response, since sometimes I get as an array and sometimes as a normal JSON object.

Currently I have done it this way: JSONObject j = new JSONObject(recvdString);

In the case of JSON array I get an error: A JSONObject text must begin with '{' at character 1 of

1

There are 1 best solutions below

0
Andrea Cip On

Is not a valid json. This one should run:

[
  "{{repeat(5, 7)}}",
  {
    "_id": "{{objectId()}}"
  }
]

However you can validate your json in a usefull services online like this