【发布时间】:2018-05-06 17:57:43
【问题描述】:
我正在尝试将 JSON 解析为 java 中的对象,JSON 是这样的
{"usage":{"text_characters":22,"features":1,"text_units":1},"entities":[{"count":1,"text":"pisos","disambiguation":{"subtype":["NONE"]},"type":"Producto"},{"count":1,"text":"No hay","disambiguation":{"subtype":["NONE"]},"type":"Quiebre_Stock"},{"count":1,"text":"madera","disambiguation":{"subtype":["NONE"]},"type":"Producto"}],"language":"es"}
我正在尝试使用此方法进行映射
parsedJsonObj = mapper.readValue(result, NLUEntitiesRelations.class);
而NLUEntitiesRelations.class就是这样
public class NLUEntitiesRelations {
private UsageNLU usage;
private List<EntityNLU> entities;
private String language;
//getter and setter
}
public class UsageNLU {
private int text_characters;
private int features;
private int text_units;
//getersand setter
}
public class EntityNLU {
private int count;
private String text;
private DisambiguationNLU disambiguation;
private String type;
//getter and setter
}
public class DisambiguationNLU {
private List<String> subtype;
//getter and setter
}
但是在执行它时,我得到了以下错误,当它是 JSON 中的 JSON 时,我小心地创建了一个新类,就像在 usagenlu 中一样
Error: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token at [Source: { "usage": { "text_units": 1, "text_characters": 22, "features": 1 }, "language": "es", "entities": [ { "type": "Producto", "text": "pisos", "disambiguation": { "subtype": [ "NONE" ] }, "count": 1 }, { "type": "Quiebre_Stock", "text": "no hay", "disambiguation": { "subtype": [ "NONE" ] }, "count": 1 }, { "type": "Producto", "text": "madera", "disambiguation": { "subtype": [ "NONE" ] }, "count": 1 } ] } ; line: 2, column: 12] (through reference chain: cl.sodimac.watson.alchemy.json.NLUEntitiesRelations["usage"])
【问题讨论】:
-
我们需要您的
UsageNLU和EntityNLU类来识别问题 -
添加了
UsageNLU和EntityNLU -
还有你的
DisambiguationNLU -
添加了
DisambiguationNLU