gpt4 book ai didi

java - 如何使用 jackson 解析这个 JSON 文件?

转载 作者:行者123 更新时间:2023-11-30 10:59:55 28 4
gpt4 key购买 nike

我有这个 JSON 文件

{
"personal": {
"name": "John Doe",
"age": "28",
"gender": "male",
}
}

这是我要解析的 POJO

public class customerInfo{
private String infoType;
private Map<String, String>;
}

我之所以要用这个POJO是因为JSON文件也可以

{
"address": {
"street": "123 main st",
"state": "md",
"zipcode": "21228"
}
}

这是我试过的,但是没有用

customerInfo customer = mapper.readValue(new File("jsonTestFile.json"), customerInfo.class);

有什么帮助吗?谢谢!

编辑:所以我希望 infoType 映射到个人或地址。而Map应该是,等等

最佳答案

您可以使用自定义反序列化器和序列化器来完成此操作。

反序列化器看起来像

public class CustomerInfoDeserializer extends JsonDeserializer<CustomerInfo> {

private final Set<String> allowedFields;

public CustomerInfoDeserializer(String... allowedFields) {
Set<String> allowed = new HashSet<>(Arrays.asList(allowedFields));
this.allowedFields = Collections.unmodifiableSet(allowed);
}

@Override
public CustomerInfo deserialize(JsonParser jp, DeserializationContext dc)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
String field = node.fieldNames().next();
if (!allowedFields.contains(field)) {
throw new JsonMappingException("Field '" + field + "' not allowed");
}

CustomerInfo info = new CustomerInfo();
info.setInfoType(field);
JsonNode dataNode = node.get(field);
for (Iterator<String> it = dataNode.fieldNames(); it.hasNext();) {
String name = it.next();
info.addProperty(name, dataNode.get(name).asText());
}

return info;
}
}

序列化器就是

public class CustomerInfoSerializer extends JsonSerializer<CustomerInfo> {

@Override
public void serialize(CustomerInfo c, JsonGenerator jg, SerializerProvider sp)
throws IOException, JsonProcessingException {
jg.writeStartObject();
jg.writeObjectField(c.getInfoType(), c.getProperties());
jg.writeEndObject();
}
}

CustomerInfo

public class CustomerInfo {

private String infoType;
private final Map<String, String> properties = new HashMap<>();

public void addProperty(String name, String value) {
properties.put(name, value);
}

public Map<String, String> getProperties() {
return properties;
}

public String getInfoType() {
return infoType;
}

public void setInfoType(String infoType) {
this.infoType = infoType;
}
}

测试

public static void main(String[] args) throws Exception {
final String personal
= "{"
+ " \"personal\": {"
+ " \"name\": \"John Doe\","
+ " \"age\": \"28\","
+ " \"gender\": \"male\""
+ " }"
+ "}";
final String address
= "{"
+ " \"address\": {"
+ " \"street\": \"123 main st\","
+ " \"state\": \"md\","
+ " \"zipcode\": \"21228\""
+ " }"
+ "}";

ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(
CustomerInfo.class,
new CustomerInfoDeserializer("personal", "address")
);
module.addSerializer(CustomerInfo.class, new CustomerInfoSerializer());
mapper.registerModule(module);

CustomerInfo info = mapper.readValue(personal, CustomerInfo.class);
System.out.println(mapper.writeValueAsString(info));

info = mapper.readValue(address, CustomerInfo.class);
System.out.println(mapper.writeValueAsString(info));
}

查看更多:

关于java - 如何使用 jackson 解析这个 JSON 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31756277/

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