I know If the file User.json has
我知道如果文件User.json有
{
"name" : { "first" : "Joe", "last" : "Sixpack" },
"gender" : "MALE",
"verified" : false,
"userImage" : "Rm9vYmFyIQ=="
}
I can construct a single User object like this :
我可以像这样构造一个User对象:
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 :
但是如果User.json文件具有以下内容,我该如何构造对象列表:
{
"name" : { "first" : "Joe", "last" : "Sixpack" },
"gender" : "MALE",
"verified" : false,
"userImage" : "Rm9vYmFyIQ=="
},
{
"name" : { "first" : "Jane", "last" : "Austen" },
"gender" : "FEMALE",
"verified" : false,
"userImage" : "DFREWEWE=="
}
?
?
1 个解决方案
#1
11
Multiple ways: if you have a JSON array of these, you can do:
多种方式:如果你有这些JSON数组,你可以这样做:
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)
并迭代值(添加到List等)
#1
11
Multiple ways: if you have a JSON array of these, you can do:
多种方式:如果你有这些JSON数组,你可以这样做:
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)
并迭代值(添加到List等)