[英]Convert complex JSON to one-level POJO using Jackson
Let's imagine you have the following JSON: 假设您有以下JSON:
{
"prop0": "value0",
"level1" : {
"prop1": "value1"
"prop2": "value2"
},
....
}
Can it be turned to simple Java object? 可以将其转换为简单的Java对象吗?
class Pojo {
private String prop0;
private String prop1;
private String prop2;
}
I don't want to create a intermediate class to wrap "level1". 我不想创建一个中间类来包装“ level1”。
What is come to my mind is to map my class in this way: 我想到的是以这种方式映射我的班级:
class Pojo {
private String prop0;
@JsonProperty("level1.prop1")
private String prop1;
@JsonProperty("level1.prop2")
private String prop2;
}
But unfortunately it doesn't work. 但不幸的是,它不起作用。 The inverse problem - turn complex Java object to plain JSON can be simply solved using @JsonUnwrapped
annotation. 反问题-使用@JsonUnwrapped
注释可以简单地解决将复杂的Java对象转换为纯JSON的问题。
Can you please suggest any workable solution for my issue? 您能为我的问题提出任何可行的解决方案吗?
You need to either write a custom deserializer, or add a setter that can transform the structure. 您需要编写自定义解串器,或添加可以转换结构的设置器。 For latter you could do something like 对于后者,您可以做类似的事情
...
public void setLevel1(Map<String,String> values) { // or JsonNode
prop1 = values.get("prop1");
// and so forth; if names are regular, can use looping
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.