[英]Deserializing json to pojo where json field has different data types
I'm trying to deserialize json to java pojo and I can't wrap my head around how should I begin to create a java object that could be used to deserialize json like this:我正在尝试将 json 反序列化为 java pojo,但我不知道应该如何开始创建一个 java object 来反序列化 json,如下所示:
{
"links": [],
"version": 2,
"executionState": "completed",
"outputs": [
{
"name": "identifier",
"value": "2411"
},
{
"name": "segment",
"value": "COMPLEX"
},
{
"name": "rules",
"value": [
{
"metadata": [
{
"ID": "int"
},
{
"NAME": "string"
},
]
},
{
"data": [
[
2,
"S1"
],
[
21,
"S6"
]
]
}
]
}
]
}
I'm working with jackson and looking to use something like this: MyPojo response = mapper.readValue(jsonString, MyPojo.class)我正在使用 jackson 并希望使用这样的东西:MyPojo response = mapper.readValue(jsonString, MyPojo.class)
The biggest struggle is those "value" elements, where some fields contain simple String and in other cases have an array of elements最大的困难是那些“值”元素,其中一些字段包含简单的字符串,而在其他情况下则包含一个元素数组
I've tried to create an abstract class for the element "value", and then 2 more classes that would extend abstract class. But that didn't work我试图为元素“值”创建一个抽象 class,然后再创建 2 个扩展抽象 class 的类。但这没有用
The best way is create a custom type resolver:最好的方法是创建自定义类型解析器:
static class Outputs {
public String name;
@JsonDeserialize(using = OutputsDeserializer.class)
public OutputsValue value;
}
static abstract class OutputsValue {
}
@AllArgsConstructor
static class Outputs1 extends OutputsValue {
public String value;
}
@AllArgsConstructor
static class Outputs2 extends OutputsValue {
public List<Outputs2Item> value;
}
static class OutputsDeserializer extends JsonDeserializer<OutputsValue> {
@Override
public OutputsValue deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
ObjectCodec codec = jp.getCodec();
JsonNode node = codec.readTree(jp);
if (node.getNodeType().toString().equalsIgnoreCase("STRING"))
return new Outputs1(codec.treeToValue(node, String.class));
List<Outputs2Item> xs = new ArrayList<>();
node.elements().forEachRemaining(e -> {
try {
xs.add(codec.treeToValue(e, Outputs2Item.class));
} catch (JsonProcessingException ex) {
ex.printStackTrace();
}
});
return new Outputs2(xs);
}
}
static class Outputs2Item {
public List<Outputs2ItemMeta> metadata;
public List<Object /* integer | string */ > data;
}
static class Outputs2ItemMeta {
public String ID;
public String NAME;
}
static class Pojo {
public List<String> links;
public int version;
public String executionState;
public List<Outputs> outputs;
}
running with与
final Pojo o = new ObjectMapper().readValue(new File("/home/josejuan/tmp/z.json"), Pojo.class);
the object contains all required values object 包含所有必需的值
You can use Jackson to parse the JSON into a JsonNode instance and create the POJOs yourself, depending on the type of the value element:您可以使用 Jackson 将 JSON 解析为 JsonNode 实例并根据值元素的类型自行创建 POJO:
final var objectMapper = new ObjectMapper();
final var jsonNode = objectMapper.readTree(json);
Then you can use the instance methods provided by JsonNode
to check the values and types and create the corresponding Java objects.然后就可以使用
JsonNode
提供的实例方法来检查值和类型,并创建相应的 Java 对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.