'get JsonArray int values inside a JsonArray in Java
I have a json file with the next estructure:
[
{
"id": 3,
"numbers": [0,1,5,3,4]
},
{
"id": 2,
"numbers": [3,7]
},
{
"id": 3,
"numbers": []
}]
And i get access to the general array and the tag "id", but when i want to access to the array surrounded by the tag "numbers" i can't get the inside values, this is my Java code
JsonArray arr = parser.parse(content).getAsJsonArray();
for (int i = 0; i < arr.size(); i++) {
JsonObject object= arr .get(i).getAsJsonObject();
String id= object.get("id").getAsString();
JsonArray numbarr = objeto.get("numbers").getAsJsonArray();
for (int j = 0; j < numbarr.size(); j++) {
JsonObject object2 = numbarr.get(i).getAsJsonObject();
}
}
how i can access to the values of the inside array and save them? The numbers can be any int value or an empty array
Solution 1:[1]
Close.
JsonArray arr = new JsonParser().parse(content).getAsJsonArray();
for (int i = 0; i < arr.size(); i++) {
JsonObject object = arr.get(i).getAsJsonObject();
String id = object.get("id").getAsString();
JsonArray numbarr = object.get("numbers").getAsJsonArray();
for (int j = 0; j < numbarr.size(); j++) {
System.out.printf("%d ", numbarr.get(j).getAsInt());
}
System.out.println();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Johnny Mopp |
