首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JsonMappingException:无法从START_OBJECT token %1反序列化java.lang.String的实例

JsonMappingException:无法从START_OBJECT token %1反序列化java.lang.String的实例
EN

Stack Overflow用户
提问于 2018-05-07 01:57:44
回答 3查看 10K关注 0票数 1

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

代码语言:javascript
复制
{"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"}

我正在尝试使用此方法进行映射

代码语言:javascript
复制
parsedJsonObj = mapper.readValue(result, NLUEntitiesRelations.class);

NLUEntitiesRelations.class就是这样的

代码语言:javascript
复制
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中那样

代码语言:javascript
复制
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"])
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-05-07 02:27:18

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

代码语言:javascript
复制
"disambiguation": {
    "subtype": [
        "NONE"
    ]
}

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

class EntityNLU中的

应该是private DisambiguationNLU disambiguation; (不带List<>)

而不是private List<DisambiguationNLU> disambiguation;

  • In你的class DisambiguationNLU

它应该是private List<String> subtype; (t小写)

代替private List<String> subType;

票数 0
EN

Stack Overflow用户

发布于 2018-05-07 02:36:12

你的领域

private List<DisambiguationNLU> disambiguation;

应该是

private DisambiguationNLU disambiguation;

它是一个对象,而不是数组

票数 0
EN

Stack Overflow用户

发布于 2018-12-29 16:36:59

解决了这个问题,它对我来说运行得很好。使用了Jackson lib并分享了我的代码片段。

代码语言:javascript
复制
   **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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50202876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档