I know If the file User.json has
{
"name" : { "first" : "Joe", "last" : "Sixpack" },
"gender" : "MALE",
"verified" : false,
"userImage" : "Rm9vYmFyIQ=="
}
I can construct a single User object like this :
User user = mapper.readValue(new File("user.json"), User.class);
But how do I construct a list of objects if the file User.json has :
{
"name" : { "first" : "Joe", "last" : "Sixpack" },
"gender" : "MALE",
"verified" : false,
"userImage" : "Rm9vYmFyIQ=="
},
{
"name" : { "first" : "Jane", "last" : "Austen" },
"gender" : "FEMALE",
"verified" : false,
"userImage" : "DFREWEWE=="
}
?
Multiple ways: if you have a JSON array of these, you can do:
User[] users = mapper.readValue(json, User[].class);
or, if it is just a sequence of root level values you can do:
Iterator<User> it = mapper.readValues(json, User.class);
and iterate over values (add to a List or such)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With