![](/img/trans.png)
[英]JSON: UnrecognizedPropertyException for List of child objects
[英]JSON array to List of objects: UnrecognizedPropertyException
我找不到以下数组 json 字符串的错误位置。 我不确定我是否正确映射。 希望有朋友帮帮我。
谢谢...
JSON 字符串值如下所示;
[
{
"PaymentRequest": {
"RequestGuid": 123
...
}
},{
"PaymentRequest": {
"RequestGuid": 456
...
}
}
]
对象定义如下;
@JsonRootName(value = "PaymentRequest")
@JsonIgnoreProperties(ignoreUnknown = true)
public class PaymentRequest{
@JsonProperty("RequestGuid")
String requestGuid;
...
}
我的包装类如下所示;
public class MyWrapper{
PaymentRequest paymentRequest;
//setter getter
}
我的实现如下。
ObjectMapper mapper = new ObjectMapper();
List<MyWrapper> users = mapper.readValue(jsonString, new TypeReference<List<MyWrapper>>() {});
结果:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“PaymentRequest”(类vpos.dto.MyWrapper),在[来源:(StringReader)处未标记为可忽略(一个已知属性:“paymentRequest”]); 行:3,列:24](通过引用链:java.util.ArrayList[0]->vpos.dto.MyWrapper["PaymentRequest"])
问题是 json 中的属性称为PaymentRequest
并且您的字段是paymentRequest
p 较低。 您可以在字段中添加注释@JsonProperty("PaymentRequest")
或更改属性命名策略,如下所示:
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.