Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over JsonObject properties

I would like to iterate through the big wrapping JsonObject with Gson. My final aim is to get an ArrayList of all existing three digit code integers in the inner "unterfeld" objects, but that will be no problem once I can iterate through the outer object's properties.

{   "something1": {     "bezeichnung": "something1",     "unterfeld": [       {         "bezeichnung": "bla1",         "unterregionen": [         ],         "code": 111       },       {         "bezeichnung": "bla2",         "unterregionen": [         ],         "code": 222       }     ],     "code": 3   },   "something2": {     "bezeichnung": "something2",     "unterfeld": [       {         "bezeichnung": "bla3",         "unterregionen": [         ],         "code": 333       }     ],     "code": 6   },   "something3": {     "bezeichnung": "something3",     "unterfeld": [       {         "bezeichnung": "bla4",         "unterregionen": [         ],         "code": 444       },       {         "bezeichnung": "bla5",         "unterregionen": [         ],         "code": 555       },       {         "bezeichnung": "bla6",         "unterregionen": [         ],         "code": 666       }     ],     "code": 9   } } 

Is there any neat way to do that?

like image 269
dotwin Avatar asked Apr 15 '12 19:04

dotwin


People also ask

How do I iterate over a JSON 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. }

Can you iterate JSON object Java?

1) Create a Maven project and add json dependency in POM. xml file. 2) Create a string of JSON data which we convert into JSON object to manipulate its data. 3) After that, we get the JSON Array from the JSON Object using getJSONArray() method and store it into a variable of type JSONArray.

What line of code is used to iterate through all the properties of an object?

Description. The loop will iterate over all enumerable properties of the object itself and those the object inherits from its prototype chain (properties of nearer prototypes take precedence over those of prototypes further away from the object in its prototype chain).

Can you loop through an object JavaScript?

Object. key(). It returns the values of all properties in the object as an array. You can then loop through the values array by using any of the array looping methods.


1 Answers

You can use entrySet to iterate over the members of the outermost JsonObject.

JsonObject object; ArrayList<Integer> codes = new ArrayList<Integer>(); for (Map.Entry<String,JsonElement> entry : object.entrySet()) {     JsonArray array = entry.getValue().getAsJsonObject().getAsJsonArray("unterfeld");     for (JsonElement codeHolder : array) {         codes.add(codeHolder.getAsJsonObject().getAsJsonPrimitive("code").getAsInt());     } } 
like image 51
David Harkness Avatar answered Sep 29 '22 01:09

David Harkness