【问题标题】:Why does my JSON mapper not recognize my object?为什么我的 JSON 映射器无法识别我的对象?
【发布时间】:2018-10-22 19:24:25
【问题描述】:
public class ResponseList implements Serializable {

    private String sku;

    private String query;

    private List<QAResponse> responses;
    // getter and setter
}

二等:

public class QAResponse implements Serializable {

    private AnswerLevel answerLevel;

    private double similarity;

    private String question;

    private String dataSource;

    private String answer;

    private String ensembleFlag;

    // getter and setter
}

我的 JSON (jsonOutput):

{  
   "sku":"4265252",
   "query":"\u8fd9\u6b3e\u662f\u5927\u4e00\u5339\u7684\u5440",
   "QAResponse":[  
      {  
         "answerLevel":"L1",
         "similarity":"1.217891",
         "question":"\u51e0\u5339\u7684",
         "dataSource":"knowledge",
         "ensembleFlag":"YES",
         "answer":"1\u5339\u7684"
      }
}

那为什么我的 JSON 对象映射器失败了?

ResponseList responseList = null;
if (jsonOutput != null) {
    ObjectMapper mapper = new ObjectMapper();
    try {
        responseList = mapper.readValue(jsonOutput, ResponseList.class);
    } catch (IOException io) {
        LOGGER.error(" json mapping to Java object failed!");
        io.printStackTrace();
    }
}

错误信息:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "QAResponse" (class com.jnlu.qe.model.ResponseList), not marked as ignorable (3 known properties: "query", "responses", "sku"])
 at [Source: (String)"{"sku": "4265252", "query": "\u8fd9\u6b3e\u662f\u5927\u4e00\u5339\u7684\u5440", "QAResponse": [{"answerLevel": "L1", "similarity": "1.217891", "question": "\u51e0\u5339\u7684", "dataSource": "knowledge", "ensembleFlag": "YES", "answer": "1\u5339\u7684"}, {"answerLevel": "L1", "similarity": "1.193976", "question": "\u8fd9\u4e2a\u662f\u51e0\u5339\u7684", "dataSource": "knowledge", "ensembleFlag": "YES", "answer": "\u8fd9\u6b3e\u662f1\u5339\u7684"}, {"answerLevel": "L1", "similarity": "1.179149", ""[truncated 8542 chars]; line: 1, column: 96] (through reference chain: com.jnlu.qe.model.ResponseList["QAResponse"])

Why doesn't the "QAResponse" not recognized?

【问题讨论】:

  • 阅读错误信息。它准确地告诉你问题是什么。您的班级有一个名为responses 的字段。您的 JSON 有一个名为 QAResponse 的字段。他们不匹配,是吗?
  • 在字段中添加@JsonProperty("QAResponse")

标签: java json fasterxml


【解决方案1】:

它抛出异常是因为在 json 输入中“QAResponse”属性不存在。如果您不想将响应更改为类,请添加 @JsonProperty 注释。

@JsonProperty(value = "QAResponse")
private List<QAResponse> responses;

【讨论】:

    【解决方案2】:

    ResponseList 类中的字段名称错误,改为:

    private List<QAResponse> responses;
    

    应该是:

    private List<QAResponse> QAResponse;
    

    但是,QAResponse.answerLevel 字段很可能是 String,除非 AnswerLevel 是枚举。

    【讨论】:

      【解决方案3】:

      我认为这是因为在 JSON 中,属性的名称为 QAResponse,但在类中它是 responses。所以你必须让它们有相同的名字

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-02
        • 2019-07-02
        • 2021-06-03
        • 2020-01-27
        • 2017-02-12
        • 1970-01-01
        • 1970-01-01
        • 2013-03-19
        相关资源
        最近更新 更多