简体   繁体   English

将JSON列表转换为POJO

[英]Turning a JSON list into a POJO

I'm having trouble getting this bit of JSON into a POJO. 我很难将JSON转换为POJO。 I'm using Jackson configured like this: 我正在使用像这样配置的Jackson:

protected ThreadLocal<ObjectMapper> jparser = new ThreadLocal<ObjectMapper>();

    public void receive(Object object) {
    try { 
        if (object instanceof String && ((String)object).length() != 0) {
            ObjectDefinition t = null ; 
            if (parserChoice==0) { 
                if (jparser.get()==null) {
                    jparser.set(new ObjectMapper());
                }
                t = jparser.get().readValue((String)object, ObjectDefinition.class);
            }

            Object key = t.getKey();
            if (key == null)
                return;
            transaction.put(key,t); 
        } 
    } catch (Exception e) { 
        e.printStackTrace();
    }
}

Here's the JSON that needs to be turned into a POJO: 这是需要转换为POJO的JSON:

{
    "id":"exampleID1",
    "entities":{
      "tags":[
         {
            "text":"textexample1",
            "indices":[
               2,
               14
            ]
         },
         {
            "text":"textexample2",
            "indices":[
               31,
               36
            ]
         },
         {
            "text":"textexample3",
            "indices":[
               37,
               43
            ]
         }
      ]
}

And lastly, here's what I currently have for the java class: 最后,这是我目前对java类所拥有的:

    protected Entities entities;
@JsonIgnoreProperties(ignoreUnknown = true)
protected class Entities {
    public Entities() {}
    protected Tags tags;
    @JsonIgnoreProperties(ignoreUnknown = true)
    protected class Tags {
        public Tags() {}

        protected String text;

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }
    };

    public Tags getTags() {
        return tags;
    }
    public void setTags(Tags tags) {
        this.tags = tags;
    }
};
//Getters & Setters ...

I've been able to translate the more simple objects into a POJO, but the list has me stumped. 我已经能够将更简单的对象转换为POJO,但是列表很麻烦。

Any help is appreciated. 任何帮助表示赞赏。 Thanks! 谢谢!

I think your issue is with your class definition. 我认为您的问题与班级定义有关。 It seems that you wish that the Tags class contains the raw text from Json, which is an array. 似乎您希望Tags类包含Json的原始文本,它是一个数组。 What I would do instead: 我会怎么做:

protected Entities entities;
@JsonIgnoreProperties(ignoreUnknown = true)
protected class Entities {
    public Entities() {}
    @JsonDeserialize(contentAs=Tag.class)
    protected List<Tag> tags;

    @JsonIgnoreProperties(ignoreUnknown = true)
    protected class Tag {
        public Tag() {}

        protected String text;

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }
    };

    public Tags getTags() {
        return tags;
    }
    public void setTags(Tags tags) {
        this.tags = tags;
    }
};

Here on the field tags I use a List to represent the Json array, and I tell Jackson to deserialize the content of that list as the Tag class. 在这里,在字段标签上,我使用List表示Json数组,并告诉Jackson将该列表的内容反序列化为Tag类。 This is required because Jackson doesn't have the runtime information of the generic declaration. 这是必需的,因为Jackson没有通用声明的运行时信息。 You'd do the same thing for the indices, namely have a field List<Integer> indices with the JsonDeserialize annotation. 您将对索引执行相同的操作,即具有JsonDeserialize批注的字段List<Integer> indices

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM