亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我的 JSON 映射器無法識別我的對象?

為什么我的 JSON 映射器無法識別我的對象?

夢里花落0921 2021-12-01 19:03:28
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?
查看完整描述

3 回答

?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

它拋出異常,因為在 json 輸入“QAResponse”屬性中不存在。如果您不想將響應更改為類,請添加 @JsonProperty 注釋。


@JsonProperty(value = "QAResponse")

private List<QAResponse> responses;


查看完整回答
反對 回復 2021-12-01
?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

ResponseList類中的字段名稱錯誤,而是:

private List<QAResponse> responses;

它應該是:

private List<QAResponse> QAResponse;

然而,QAResponse.answerLevel字段很可能是一個String除非AnswerLevel是一個枚舉。


查看完整回答
反對 回復 2021-12-01
?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

我認為這是因為在 JSON 中一個屬性有一個名稱,QAResponse但在一個類中它是一個responses. 所以你必須讓它們具有相同的名稱


查看完整回答
反對 回復 2021-12-01
  • 3 回答
  • 0 關注
  • 272 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號