JsonMappingException:无法从START_OBJECT令牌1中反序列化java.lang.String实例 [英] JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token 1

查看:177
本文介绍了JsonMappingException:无法从START_OBJECT令牌1中反序列化java.lang.String实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JSON解析为Java中的对象,JSON就是这样

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);

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
}

但是在执行它时,出现以下错误,并且在usenlu中它是JSON中的JSON时,我小心地创建了一个新类

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"])

推荐答案

在JSON的实体部分中,有类似以下内容:

In the entity parts of your JSON there are things like this:

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

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

That means there are 2 issues with your Java classes making them incompatible with your JSON content:

  • 在您的类EntityNLU
    中应该是 private DisambiguationNLU消除歧义; (没有 List<> )
    而不是 private List< DisambiguationNLU>消歧;
  • 在您的类DisambiguationNLU
    它应该是 private List< String>子类型; (小写的 t )
    而不是 private List< String>subType;
  • In your class EntityNLU
    it should be private DisambiguationNLU disambiguation; (without List<>)
    instead of private List<DisambiguationNLU> disambiguation;
  • In your class DisambiguationNLU
    it should be private List<String> subtype; (wth lower-case t)
    instead of private List<String> subType;

这篇关于JsonMappingException:无法从START_OBJECT令牌1中反序列化java.lang.String实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆