【发布时间】:2019-02-28 20:06:56
【问题描述】:
我正在尝试将 JSON 转换为 Java 对象,但我很难构建 Java 等效对象。
我的 JSON 看起来像这样
{
"point1": {
"x": 1.0,
"y": 2.0
},
"point2": {
"x": 1.0,
"y": 2.0
},
"point3": {
"x": 1.0,
"y": 2.0
},
"customobject1": "cust1",
"customobject2": "cust2"
}
这里需要取地图来取点,因为会有n个点,
public class Test {
public String getCustomobject1() {
return customobject1;
}
public void setCustomobject1(String customobject1) {
this.customobject1 = customobject1;
}
public String getCustomobject2() {
return customobject2;
}
public void setCustomobject2(String customobject2) {
this.customobject2 = customobject2;
}
Map<String, Point> testing = new HashMap<>();
String customobject1;
String customobject2;
public Map<String, Point> getTesting() {
return testing;
}
public void setTesting(Map<String, Point> testing) {
this.testing = testing;
}
}
但是我遇到了无法识别的属性异常,我知道有一个额外的包装器 ({}) 会导致此问题,有人可以建议我在反序列化 JSON 时如何忽略此地图名称吗?
注意:我正在工作的实际对象有点复杂,结构相似,我在这里只发布一个原型。
【问题讨论】:
-
您的“pointX”实际上是根对象的属性,所以杰克逊绝对不会将它们映射到“测试”属性。也许其他问题的答案会对您有所帮助:stackoverflow.com/questions/31164625/…。您可以将它们分配给自己在 setter 中映射
-
@uaraven 感谢您提供信息。
标签: java jackson jackson-databind