Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through javax.json.JsonArray

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.

like image 830
khateeb Avatar asked Oct 12 '25 11:10

khateeb


1 Answers

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);
}
like image 69
Leos Literak Avatar answered Oct 14 '25 23:10

Leos Literak