我有两个列表的数组。每个列表包含对象。第一个列表中的对象与第二个列表中的对象不同。在JSON中,它看起来像这样:
[
{
"domains":
[
{
"attrb1": "aaaa",
"attrb2": "bbbb"
},
{
"attrb1": "cccc",
"attrb2": "dddd"
}
]
},
{
"terms":
[
{
"attrb1": "cccc",
"attrb2": "dddd"
}
]
}
]
我将这个JSON字符串保存到文件initial-elements-v02.json中,我正在考虑这样的事情来反序列化它:
final ObjectMapper mapper = new ObjectMapper();
List<List<Object>> glossaryElements = null;
try {
glossaryElements = mapper.readValue(
arg0.resourceAsStream("initial-elements-v02.json"),
new TypeReference<ListList<<Object>>>() {
});
谢谢! 叶夫
答案 0 :(得分:1)
根据Data Binding with Generics,我会从最里面到外面建造
Map<String, String>
List<Map<String, String>>
Map<String, List<Map<String, String>>>
List<Map<String, List<Map<String, String>>>>
然后使用此
List<Map<String, List<Map<String, String>>>> glossaryElements = null;
glossaryElements = mapper.readValue(
arg0.resourceAsStream("initial-elements-v02.json"),
new TypeReference<List<Map<String, List<Map<String, String>>>>>() {});
如果initial-elements-v02.json
是文件名,我认为您也可以使用
new File("initial-elements-v02.json")