我试图将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"])
答案 0 :(得分:0)
在JSON的实体部分中有以下内容:
"disambiguation": {
"subtype": [
"NONE"
]
}
这意味着您的Java类存在两个问题,使它们与您的JSON内容不兼容:
class EntityNLU
中
它应该是private DisambiguationNLU disambiguation;
(没有List<>
)private List<DisambiguationNLU> disambiguation;
class DisambiguationNLU
中
它应该是private List<String> subtype;
(小写t
)private List<String> subType;
答案 1 :(得分:0)
你的领域
private List<DisambiguationNLU> disambiguation;
应该是
private DisambiguationNLU disambiguation;
这是一个对象,而不是数组
答案 2 :(得分:0)
解决了这个问题,对我来说很好。使用过Jackson的lib和共享我的代码段。
**MainClass**
public class MainClass {
public static void main(String[] args) throws JsonParseException,
JsonMappingException, IOException {
String jsonStr = "{\r\n" + " \"usage\": {\r\n" + " \"text_characters\": 22,\r\n"
+ " \"features\": 1,\r\n" + " \"text_units\": 1\r\n" + " },\r\n" + " \"entities\": [{\r\n"
+ " \"count\": 1,\r\n" + " \"text\": \"pisos\",\r\n" + " \"disambiguation\": {\r\n"
+ " \"subtype\": [\"NONE\"]\r\n" + " },\r\n" + " \"type\": \"Producto\"\r\n"
+ " }, {\r\n" + " \"count\": 1,\r\n" + " \"text\": \"No hay\",\r\n"
+ " \"disambiguation\": {\r\n" + " \"subtype\": [\"NONE\"]\r\n" + " },\r\n"
+ " \"type\": \"Quiebre_Stock\"\r\n" + " }, {\r\n" + " \"count\": 1,\r\n"
+ " \"text\": \"madera\",\r\n" + " \"disambiguation\": {\r\n"
+ " \"subtype\": [\"NONE\"]\r\n" + " },\r\n" + " \"type\": \"Producto\"\r\n"
+ " }],\r\n" + " \"language\": \"es\"\r\n" + "}";
ObjectMapper mapper = new ObjectMapper();
MyPojo details = mapper.readValue(jsonStr, MyPojo.class);
System.out.println("Value for getText_characters is: " + details.getUsage().getText_characters());
System.out.println("Value for getFeatures is: " + details.getUsage().getFeatures());
System.out.println("Value for getLanguage is: " + details.getLanguage().toString() + "\n");
for (Entities itr : details.getEntities()) {
System.out.println("Value for getCount is: " + itr.getCount());
System.out.println("Value for getText is: " + itr.getText());
System.out.println("Value for getText is: " + itr.getType() + "\n");
for (String itrs : itr.getDisambiguation().getSubtype()) {
System.out.println("Value for getSubtype is: " + itrs + "\n");
} } } }
**MyPojo.java**
import java.util.ArrayList;
public class MyPojo {
private Usage usage;
private String language;
private ArrayList<Entities> entities;
public Usage getUsage() {
return usage;
}
public void setUsage(Usage usage) {
this.usage = usage;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public ArrayList<Entities> getEntities() {
return entities;
}
public void setEntities(ArrayList<Entities> entities) {
this.entities = entities;
} }
**Entities class**
public class Entities {
private Disambiguation disambiguation;
private String text;
private String count;
private String type;
public Disambiguation getDisambiguation() {
return disambiguation;
}
public void setDisambiguation(Disambiguation disambiguation) {
this.disambiguation = disambiguation;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
} }
**Disambiguation**
public class Disambiguation {
private String[] subtype;
public String[] getSubtype() {
return subtype;
}
public void setSubtype(String[] subtype) {
this.subtype = subtype;
} }
**Usage.java**
public class Usage {
private String features;
private String text_characters;
private String text_units;
public String getFeatures() {
return features;
}
public void setFeatures(String features) {
this.features = features;
}
public String getText_characters() {
return text_characters;
}
public void setText_characters(String text_characters) {
this.text_characters = text_characters;
}
public String getText_units() {
return text_units;
}
public void setText_units(String text_units) {
this.text_units = text_units;
} }
**RESULTS**:
Value for getText_characters is: 22
Value for getFeatures is: 1
Value for getLanguage is: es
Value for getCount is: 1
Value for getText is: pisos
Value for getText is: Producto
Value for getSubtype is: NONE
Value for getCount is: 1
Value for getText is: No hay
Value for getText is: Quiebre_Stock
Value for getSubtype is: NONE
Value for getCount is: 1
Value for getText is: madera
Value for getText is: Producto
Value for getSubtype is: NONE