我知道如果文件User.json有
{
"name" : { "first" : "Joe", "last" : "Sixpack" },
"gender" : "MALE",
"verified" : false,
"userImage" : "Rm9vYmFyIQ=="
}
我可以像这样构建一个User对象:
User user = mapper.readValue(new File("user.json"), User.class);
但是如果文件User.json有:
,我该如何构造一个对象列表{
"name" : { "first" : "Joe", "last" : "Sixpack" },
"gender" : "MALE",
"verified" : false,
"userImage" : "Rm9vYmFyIQ=="
},
{
"name" : { "first" : "Jane", "last" : "Austen" },
"gender" : "FEMALE",
"verified" : false,
"userImage" : "DFREWEWE=="
}
?
答案 0 :(得分:11)
多种方式:如果你有这些JSON数组,你可以这样做:
User[] users = mapper.readValue(json, User[].class);
或者,如果它只是一系列根级别值,您可以这样做:
Iterator<User> it = mapper.readValues(json, User.class);
并迭代值(添加到List等)