4

I have a JsonArray and I am using the implementation as given in Java EE 7. How do I iterate thorugh this JsonArray?

I have searched thorugh this site but have found only answer which relate to org.json.JSONArray (like this Accessing members of items in a JSONArray with Java) which I cannot use as javax.json.JsonArray does not have the length method.

Community
  • 1
  • 1
khateeb
  • 5,265
  • 15
  • 58
  • 114

1 Answers1

4

Looking to javadoc will show that it extends List, so you can iterate with for loop:

javax.json.JsonArray value = javax.json.Json.createArrayBuilder()
     .add(javax.json.Json.createObjectBuilder()
             .add("type", "home")
             .add("number", "212 555-1234"))
     .add(javax.json.Json.createObjectBuilder()
             .add("type", "fax")
             .add("number", "646 555-4567"))
     .build();

for (javax.json.JsonValue jsonValue : value) {
    System.out.println(jsonValue);
}
Leos Literak
  • 8,805
  • 19
  • 81
  • 156