gpt4 book ai didi

java - 使用任意键将 JSON 反序列化为 Map

转载 作者:行者123 更新时间:2023-11-30 03:54:28 35 4
gpt4 key购买 nike

我有一个不可变的 JSON,其结构如下

{
"data": "mydata",
"information": [
{
"info": "mnkjshdflkj",
"stuff": "things"
},
{
"info": "khsjfdaksdfh",
"stuff": "more things"
}
]
}

我正在使用 Jackson 进行反序列化。我想要实现的是将这些信息存储在这种格式的 POJO 中。

MyData.java

public class MyData {
String data;
HashMap<String, Info> information;
}

信息

public class Info {
String info; // is it required?
String stuff;
}

我的 map 键将是信息字段中的值,而值将是类本身。 jackson 对这个过程的处理方法是什么?

最佳答案

如果我理解正确的话,这应该可行。首先为信息字段创建自定义反序列化器。

public class CustomDeserializer extends JsonDeserializer<Map<String, Info>> {
@Override
public Map<String, Info> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
List<Info> raw = jp.readValueAs(new TypeReference<List<Info>>() {
});
Map<String, Info> result = new HashMap<>();
for (Info info : raw) {
result.put(info.getInfo(), info);
}

return result;
}
}

那么你需要注册这个解串器。您可以借助信息字段上的注释来完成此操作。或者以不同的方式注册解串器。

@JsonDeserialize(using = CustomDeserializer.class)
Map<String, Info> information;

以及要测试的完整代码:

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.type.TypeReference;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
public static void main(String[] args) throws IOException {
String json = "{\n" +
" \"data\": \"mydata\",\n" +
" \"information\": [\n" +
" {\n" +
" \"info\": \"mnkjshdflkj\",\n" +
" \"stuff\": \"things\"\n" +
" },\n" +
" {\n" +
" \"info\": \"khsjfdaksdfh\",\n" +
" \"stuff\": \"more things\"\n" +
" }\n" +
" ]\n" +
"}";

ObjectMapper mapper = new ObjectMapper();
MyData data = mapper.readValue(json, MyData.class);
System.out.println(data);
}

public static class MyData {
String data;
@JsonDeserialize(using = CustomDeserializer.class)
Map<String, Info> information;

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}

public Map<String, Info> getInformation() {
return information;
}

public void setInformation(Map<String, Info> information) {
this.information = information;
}

@Override
public String toString() {
return "MyData{" +
"data='" + data + '\'' +
", information=" + information +
'}';
}
}

public static class Info {
String info; // is it required?
String stuff;

public String getInfo() {
return info;
}

public void setInfo(String info) {
this.info = info;
}

public String getStuff() {
return stuff;
}

public void setStuff(String stuff) {
this.stuff = stuff;
}

@Override
public String toString() {
return "Info{" +
"info='" + info + '\'' +
", stuff='" + stuff + '\'' +
'}';
}
}

public static class CustomDeserializer extends JsonDeserializer<Map<String, Info>> {
@Override
public Map<String, Info> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
List<Info> raw = jp.readValueAs(new TypeReference<List<Info>>() {
});
Map<String, Info> result = new HashMap<>();
for (Info info : raw) {
result.put(info.getInfo(), info);
}

return result;
}
}
}

关于java - 使用任意键将 JSON 反序列化为 Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23608411/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com