Reputation: 101
I would like to iterate over an array in this Json object:
{
"id": "2234",
"Messages": [
{
"MessageId": 321231239,
"Text_message": "my text message",
"date": 1444666348
},
{
"MessageId": 3217437239,
"Text_message": "my text message 2",
"date": 1444666348
}
]
}
in my code I have:
JsonArray messagesJson = jsonObject.getJsonArray("Messages");
my imports related to this are:
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
Now I want to iterate over this array and extract fields for my bean. But I don't want to use the for loop with index. I want to use a foreach type of loop. I am working with Java 7. What I want is something that is more like the Scala way of doing it:
messagesJson.map { json =>
MyBean(
(json \ "MessageId").getOrElse(""),
(json \ "text_message").getOrElse(""),
(json \ "date").getOrElse("")
)
}
Upvotes: 0
Views: 2892
Reputation: 223
As the documentation says, JsonArray
extends List<JsonValue>
so you can simply iterate over JsonValue
.
for(JsonValue value : yourJsonArray){
....
}
Upvotes: 2