JSON挑剔的Java
我有一个简单的问题,假设我有这样的json
{
"outer-field":{
"outer-a":"something",
"outer-b":12345678,
"inner-field":{
"inner-a":false,
"inner-b":0.0,
"inner-c":29.99
}
}
}
映射:
public class OuterObject {
@JsonProperty("outer-a")
public String outerA;
@JsonProperty("outer-b")
public Integer outerB;
@JsonProperty("inner-field")
public InnerObject innerField;
}
public class InnerObject{
@JsonProperty("inner-a")
public Boolean innerA;
@JsonProperty("inner-b")
public Double innerB;
@JsonProperty("inner-c")
public Double innerC;
我想知道是否可以通过使用一些自定义setter/entotation/shots或其他内容来保存内部对象中的外部字段:
public class InnerObject{
@JsonProperty("inner-a")
public Boolean innerA;
@JsonProperty("inner-b")
public Double innerB;
@JsonProperty("inner-c")
public Double innerC;
//how to map this?
@JsonProperty("outer-a")
public String outerA;
ps:使用自定义由于JSON的复杂性,次要化是我的最后一项手段
i have a simple question, let's say I have this json
{
"outer-field":{
"outer-a":"something",
"outer-b":12345678,
"inner-field":{
"inner-a":false,
"inner-b":0.0,
"inner-c":29.99
}
}
}
Mapped this way:
public class OuterObject {
@JsonProperty("outer-a")
public String outerA;
@JsonProperty("outer-b")
public Integer outerB;
@JsonProperty("inner-field")
public InnerObject innerField;
}
public class InnerObject{
@JsonProperty("inner-a")
public Boolean innerA;
@JsonProperty("inner-b")
public Double innerB;
@JsonProperty("inner-c")
public Double innerC;
I was wondering if it was possible to just save an outer field in the inner object like this by using some custom setter/annotation or something:
public class InnerObject{
@JsonProperty("inner-a")
public Boolean innerA;
@JsonProperty("inner-b")
public Double innerB;
@JsonProperty("inner-c")
public Double innerC;
//how to map this?
@JsonProperty("outer-a")
public String outerA;
PS: using a custom deserialization is my last resort due to the complexity of the json
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果有人问我如何解决问题,我决定通过使用内部的xxx方法使用自定义构建器将外部属性传递给创建期间的内部对象
If anyone was asking how I solve my problem, I decided to use a custom Builder by using the withXXX methods inside to pass the outer attributes to the inner object during creation