我需要使用 jackson 将 JSON 数组反序列化为 HashMap
I need to deserialize a JSON array to a HashMap with jackson
我有一个 json 文件,我需要将其反序列化为 HashMap。
我需要一个类型为{name, HashMap}的HashMap
示例:{aaa={value1=111, value2=222, value3=333}, bbb={value1=444, value2=555, value3=666}}
不幸的是,我是 json 和 jackson 的新手。
提前致谢。
这是我的 json
[
{
"name": "aaa",
"values" : {
"value1" : 111,
"value2" : 222,
"value3" : 333
}
},
{
"name": "bbb",
"values" : {
"value1" : 444,
"value2" : 555,
"value3" : 666
}
}
]
和我的 类:
public class Elements {
@JsonProperty("name")
String name;
@JsonProperty("values")
Values values;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Values getValues() {
return this.values;
}
public void setValues(Values values) {
this.values = values;
}
}
public class Values {
@JsonProperty("value1")
int value1;
@JsonProperty("value2")
int value2;
@JsonProperty("value3")
int value3;
public int getValue1() {
return this.value1;
}
public void setValue1(int value1) {
this.value1 = value1;
}
public int getValue2() {
return this.value2;
}
public void setValue2(int value2) {
this.value2 = value2;
}
public int getValue3() {
return this.value3;
}
public void setValue3(int value3) {
this.value3 = value3;
}
}
只需将其作为 Map
个实例的 List
读取,并在反序列化过程后将其收集到新的 Map
中:
List<Map<String, Object>> listOfMaps = mapper.readValue(json, new TypeReference<List<Map<String, Object>>>() {});
Map<String, Object> desiredMap = listOfMaps.stream().collect(HashMap::new,
(result, map) -> result.put(map.get("name").toString(), map.get("values")),
(existing, replacement) -> { });
我对@Michal 的回答投了赞成票,因为它真的很酷并且可以满足您的要求:
I need a HashMap of type {name, HashMap<valuename, value>}
如果您需要 HashMap 的 HashMap 中的数据,我不明白您定义 Elements
和 Values
类 的目的。
出于好奇,我用你的 类 测试了 Jackson,它工作得很好:
// Deserialize as a list
List<Elements> listElements = objectMapper.readValue(JSON, new TypeReference<List<Elements>>(){});
// print elements
listElements.forEach(System.out::println);
我有一个 json 文件,我需要将其反序列化为 HashMap。
我需要一个类型为{name, HashMap
示例:{aaa={value1=111, value2=222, value3=333}, bbb={value1=444, value2=555, value3=666}}
不幸的是,我是 json 和 jackson 的新手。
提前致谢。
这是我的 json
[
{
"name": "aaa",
"values" : {
"value1" : 111,
"value2" : 222,
"value3" : 333
}
},
{
"name": "bbb",
"values" : {
"value1" : 444,
"value2" : 555,
"value3" : 666
}
}
]
和我的 类:
public class Elements {
@JsonProperty("name")
String name;
@JsonProperty("values")
Values values;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Values getValues() {
return this.values;
}
public void setValues(Values values) {
this.values = values;
}
}
public class Values {
@JsonProperty("value1")
int value1;
@JsonProperty("value2")
int value2;
@JsonProperty("value3")
int value3;
public int getValue1() {
return this.value1;
}
public void setValue1(int value1) {
this.value1 = value1;
}
public int getValue2() {
return this.value2;
}
public void setValue2(int value2) {
this.value2 = value2;
}
public int getValue3() {
return this.value3;
}
public void setValue3(int value3) {
this.value3 = value3;
}
}
只需将其作为 Map
个实例的 List
读取,并在反序列化过程后将其收集到新的 Map
中:
List<Map<String, Object>> listOfMaps = mapper.readValue(json, new TypeReference<List<Map<String, Object>>>() {});
Map<String, Object> desiredMap = listOfMaps.stream().collect(HashMap::new,
(result, map) -> result.put(map.get("name").toString(), map.get("values")),
(existing, replacement) -> { });
我对@Michal 的回答投了赞成票,因为它真的很酷并且可以满足您的要求:
I need a HashMap of type {name, HashMap<valuename, value>}
如果您需要 HashMap 的 HashMap 中的数据,我不明白您定义 Elements
和 Values
类 的目的。
出于好奇,我用你的 类 测试了 Jackson,它工作得很好:
// Deserialize as a list
List<Elements> listElements = objectMapper.readValue(JSON, new TypeReference<List<Elements>>(){});
// print elements
listElements.forEach(System.out::println);