问题
I'm trying to parse a JSON to an object in java, the JSON is something like that
{"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"}
I'm trying to mapping with this method
parsedJsonObj = mapper.readValue(result, NLUEntitiesRelations.class);
and NLUEntitiesRelations.class
is like that
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
}
but when executing it I am given the following error, and I was careful to create a new class when it was a JSON within JSON as in 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"])
回答1:
In the entity parts of your JSON there are things like this:
"disambiguation": {
"subtype": [
"NONE"
]
}
That means there are 2 issues with your Java classes making them incompatible with your JSON content:
- In your
class EntityNLU
it should beprivate DisambiguationNLU disambiguation;
(withoutList<>
)
instead ofprivate List<DisambiguationNLU> disambiguation;
- In your
class DisambiguationNLU
it should beprivate List<String> subtype;
(wth lower-caset
)
instead ofprivate List<String> subType;
回答2:
Your field
private List<DisambiguationNLU> disambiguation;
should be
private DisambiguationNLU disambiguation;
It's an object, not array
回答3:
Resolved this problem and it's working fine for me. Have used Jackson lib and Sharing my code snippets.
**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
来源:https://stackoverflow.com/questions/50202876/jsonmappingexception-can-not-deserialize-instance-of-java-lang-string-out-of-st