【发布时间】:2018-04-25 07:14:59
【问题描述】:
之前可能会在这里问过类似的问题,但我没有运气,我想知道如何从下面的 json 字符串中提取特定对象,如 user,然后构造一个 ArrayList。不过有一个转折点,Users 下的属性之一是随机数,可以是任何东西!!!
这是我的 json 字符串的样子:
<code>{
"_links": {
},
"count": {
},
"users": {
"123321": { //*Is a random number which can be any number
"_links": {
},
"user": {
"id": "123321",
"name": "...",
"age": "...",
"address": ""
..
}
},
"456654": {
"_links": {
},
"user": {
"id": "456654",
"name": "...",
"age": "...",
"address": ""
...
}
}
...
},
"page": {
}
}
</code>
我想把它转换成的java对象是:
@JsonIgnoreProperties(ignoreUnknown = true) // Ignore any properties not bound here
public class User {
private String id;
private String name;
//setter:getter
}
注意:转换应该只考虑这两个字段 (id,name),并忽略来自 json 响应 user:{} 对象的其余字段。
理想情况下,我希望得到这样的列表:
List<User> users = resulted json transformation should return a list of users!!
您知道我该如何使用 Jackson JSON Parser/或者 GSON 来做到这一点吗?
【问题讨论】:
-
不是真的因为我有一个随机的对象名称,可以是任何东西!
-
这个答案也没有显示如何构造
List<User>列表!
标签: java json spring-boot jackson gson