当前位置: 首页 > 知识库问答 >
问题:

JSONMappingException:无法将START_OBJECT令牌1中的java.lang.String实例反序列化

廖臻
2023-03-14
{"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"])

共有1个答案

庾鸿飞
2023-03-14

在JSON的实体部分中有如下内容:

"disambiguation": {
    "subtype": [
        "NONE"
    ]
}

这意味着Java类存在两个问题,使它们与JSON内容不兼容:

  • 类entitynlu
    中,应该是私有消歧nlu消歧;(没有列表<>)
    而不是私有列表 消歧;
  • 类disambiguationnlu
    中,它应该是私有列表 子类型; (小写t)
    而不是私有列表 子类型;
 类似资料: