【问题标题】:JSON array to List of objects: UnrecognizedPropertyExceptionJSON数组到对象列表:UnrecognizedPropertyException
【发布时间】:2020-03-09 13:30:39
【问题描述】:

我找不到以下数组 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),未标记为可忽略(一个已知属性:“paymentRequest”]) 在 [来源:(StringReader); line: 3, column: 24] (通过引用链: java.util.ArrayList[0]->vpos.dto.MyWrapper["PaymentRequest"])

【问题讨论】:

  • 在 MyWrapper 类中应该是 List 而不是 PaymentRequest
  • 什么是QNBWrapper?这与MyWrapper 有关还是相同但只是一个错字?如果是这样,那么您可能希望将 @JsonProperty("PaymentRequest") 添加到 PaymentRequest paymentRequest;(并删除该 @JsonRootName(value = "PaymentRequest") - 尽管我不熟悉该注释,所以我可能在这里错了)。
  • 我试过 "List PaymentRequest" 和 "List paymentRequests" ,但我得到了同样的错误。
  • 谢谢兄弟们,我把它改成了这样:它现在可以工作了。非常感谢。 @JsonProperty("PaymentRequest") private PaymentRequest paymentRequest;

标签: java json jackson


【解决方案1】:

问题是json中的属性称为PaymentRequest,而您的字段是paymentRequest,开头的p较低。您可以将注释 @JsonProperty("PaymentRequest") 添加到您的字段或更改属性命名策略,如下所示:

ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 2020-03-31
    • 1970-01-01
    相关资源
    最近更新 更多