0

In reference to JSON Array iteration in Android/Java, when I have a single array

[{json object},{json object},{json object}]

I could easily create a JSONArray from the string as below.

int id;
String name;
JSONArray array = new JSONArray(string_of_json_array);
for (int i = 0; i < array.length(); i++) {
    JSONObject row = array.getJSONObject(i);
    id = row.getInt("id");
    name = row.getString("name");
}

However, if I have JSON String of multiple array

[{json object},{json object},{json object}]
[{json object},{json object},{json object}]
[{json object},{json object},{json object}]

Can I still fit into a single JSONArray (i.e. making it JSONArray of JSONArray)? If not, how could I split them into multiple JSONArray (one for each of the row above)?

Thank you.

Community
  • 1
  • 1
Elye
  • 53,639
  • 54
  • 212
  • 474

1 Answers1

0

You can do something like this:

JSONObject data = json.getJSONObject("KEY");

 JSONArray arrayOfArrays = data.getJSONArray("KEY_2");

 int size = arrayOfArrays.length();

 for (int i = 0; i < size; i++) {
  yourObject = arrayOfArrays.getJSONArray(i).getString("Key_3");
}
Victor Viola
  • 575
  • 1
  • 4
  • 14