I have a JSON response like this.
{
"array": [
{
"object1": {
"aa": "somevalue1",
"bb": "somevalue2",
"cc": "somevalue3"
}
},
{
"object2": {
"aa": "somevalue4",
"bb": "somevalue5",
"cc": "somevalue6"
}
}
]}
Now I can get a JSON array from above response. I want read the value aa
,bb
,cc
from object object1
and object2
in a for loop
.
JSON is dynamic, I mean that object2
can also appear before object1
(in reverse order) and there is chances that I might get one more object(object3
) with same structure OR only one (object1
or object2
or object3
).
I dont want to do like this as I failed in :
JSONArray jsonArray = json.getJSONArray("array");
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject jsonObject = jsonArray.getJSONObject(i).getJSONObject("object1");
}
So my question is HOW can I read those values aa
,bb
,cc
without depending on object name or number of object (object1
or object2
or object3
)?