Erik Erik - 1 year ago 319
Java Question

JSON - Iterate through JSONArray

I have a JSON file with some arrays in it. I want to iterate through the file arrays and get their elements and their values.

This is how my file looks like:

"JObjects": {
"JArray1": [
"A": "a",
"B": "b",
"C": "c"
"A": "a1",
"B": "b2",
"C": "c3",
"D": "d4"
"E": "e5"
"A": "aa",
"B": "bb",
"C": "cc",
"D": "dd"



This is how far I have come:

JSONObject object = new JSONObject("json-file.json");
JSONObject getObject = object.getJSONObject("JObjects");
JSONArray getArray = getObject.getJSONArray("JArray1");

for(int i = 0; i < getArray.size(); i++)
JSONObject objects = getArray.getJSONArray(i);
//Iterate through the elements of the array i.
//Get thier value.
//Get the value for the first element and the value for the last element.

Is it possible to do something like this?

The reason I want to do it like this is because the arrays in the file have a different number of elements.

Answer Source


JSONObject objects = getArray.getJSONArray(i);


JSONObject objects = getArray.getJSONObject(i);

or to

JSONObject objects = getArray.optJSONObject(i);

depending on which JSON-to/from-Java library you're using. (It looks like getJSONObject will work for you.)

Then, to access the string elements in the "objects" JSONObject, get them out by element name.

String a = objects.get("A");

If you need the names of the elements in the JSONObject, you can use the static utility method JSONObject.getNames(JSONObject) to do so.

String[] elementNames = JSONObject.getNames(objects);

"Get the value for the first element and the value for the last element."

If "element" is referring to the component in the array, note that the first component is at index 0, and the last component is at index getArray.length() - 1.

I want to iterate though the objects in the array and get thier component and thier value. In my example the first object has 3 components, the scond has 5 and the third has 4 components. I want iterate though each of them and get thier component name and value.

The following code does exactly that.

import org.json.JSONArray;
import org.json.JSONObject;

public class Foo
  public static void main(String[] args) throws Exception
    String jsonInput = "{\"JObjects\":{\"JArray1\":[{\"A\":\"a\",\"B\":\"b\",\"C\":\"c\"},{\"A\":\"a1\",\"B\":\"b2\",\"C\":\"c3\",\"D\":\"d4\",\"E\":\"e5\"},{\"A\":\"aa\",\"B\":\"bb\",\"C\":\"cc\",\"D\":\"dd\"}]}}";

    // "I want to iterate though the objects in the array..."
    JSONObject outerObject = new JSONObject(jsonInput);
    JSONObject innerObject = outerObject.getJSONObject("JObjects");
    JSONArray jsonArray = innerObject.getJSONArray("JArray1");
    for (int i = 0, size = jsonArray.length(); i < size; i++)
      JSONObject objectInArray = jsonArray.getJSONObject(i);

      // "...and get thier component and thier value."
      String[] elementNames = JSONObject.getNames(objectInArray);
      System.out.printf("%d ELEMENTS IN CURRENT OBJECT:\n", elementNames.length);
      for (String elementName : elementNames)
        String value = objectInArray.getString(elementName);
        System.out.printf("name=%s, value=%s\n", elementName, value);
name=A, value=a
name=B, value=b
name=C, value=c

name=D, value=d4
name=E, value=e5
name=A, value=a1
name=B, value=b2
name=C, value=c3

name=D, value=dd
name=A, value=aa
name=B, value=bb
name=C, value=cc
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download