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?
答案 0 :(得分:4)
它抛出异常,因为在json输入中不存在“ QAResponse”属性。如果您不想将响应更改为类,请添加@JsonProperty批注。
@JsonProperty(value = "QAResponse")
private List<QAResponse> responses;
答案 1 :(得分:2)
ResponseList
类中的字段名错误,相反:
private List<QAResponse> responses;
应该是:
private List<QAResponse> QAResponse;
但是,除非QAResponse.answerLevel
是一个枚举,否则String
字段很可能应该是AnswerLevel
。
答案 2 :(得分:1)
我认为这是因为JSON中的属性名称为QAResponse
,而在类中则为responses
。因此,您必须使它们具有相同的名称