Gson – Loop & iterate JSON Array example shows how to loop JSON array using the Gson library in Java. The example also shows how to iterate over a JSON array using the for loop.
How to iterate JSON array using the for loop?
For this example, I’m going to use the below given Employee JSON data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
{ "Id": "EMP001", "Name": "Alex", "Age": 22, "IsManager": false, "ReservedParking": null, "Languages": ["English", "Spanish"], "Experience": [ { "CompanyName": "ABC Ltd.", "Years": 2.5 }, { "CompanyName": "XYZ Ltd.", "Years": 3 } ] } |
The “Experience” property contains an array that has relevant companies as individual array elements. To iterate this array using the for loop, first of all, we need to parse this JSON into JsonObject.
Once we get the JsonObject, we can get the array of JSON objects contained in the array and iterate over it using the for loop as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.javacodeexamples.gsonexamples; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; public class GsonIterateJsonArrayExample { public static void main(String[] args) { String jsonData = "{ \"Id\": \"EMP001\", " + "\"Name\": \"Alex\", \"Age\": 22, " + "\"IsManager\": false, " + "\"ReservedParking\": null, " + "\"Languages\": [\"English\", \"Spanish\"], " + "\"Experience\": [ { \"CompanyName\": \"ABC Ltd.\", \"Years\": 2.5 }, { \"CompanyName\": \"XYZ Ltd.\", \"Years\": 3 } ] " + "}"; //parse and get the JsonObject JsonObject jsonObject = JsonParser.parseString(jsonData).getAsJsonObject(); //get the "Experience" JSON array JsonArray experienceArray = jsonObject.get("Experience").getAsJsonArray(); for(JsonElement experienceElement : experienceArray) { /* * The "Experience" array contains individual * company objects as elements, so we need to * get them as JsonObject */ JsonObject companyObject = experienceElement.getAsJsonObject(); System.out.println(companyObject.get("CompanyName") + " => " + companyObject.get("Years")); } } } |
Output
1 2 |
"ABC Ltd." => 2.5 "XYZ Ltd." => 3 |
In the above example, the “Experience” JSON array contained the company JSON objects as its elements. So we used the getAsJsonObject
method for each element of an array. We have to use the corresponding methods according to the JSON array element data type.
For example, the “Languages” array contains string values as elements. For these values, we just need to use the getAsString
method inside the for loop while iterating as given below.
1 2 3 4 5 6 7 8 9 10 |
//parse and get the JsonObject JsonObject jsonObject = JsonParser.parseString(jsonData).getAsJsonObject(); //get the "Languages" JSON array JsonArray languageArray = jsonObject.get("Languages").getAsJsonArray(); //iterate over languages for(JsonElement langElement : languageArray) { System.out.println(langElement.getAsString()); } |
Output
1 2 |
English Spanish |
Please let me know your views in the comments section below.