简体   繁体   English

Jackson JSON从没有属性名称的对象数组转换为pojo

[英]Jackson JSON to pojo from array of objects with no property name

Considering this structure, what is the correct notation for getting the array of objects (property, type fields) inside of the parent property. 考虑到这种结构,在父属性内获取对象数组(属性,类型字段)的正确表示法是什么。

{"parent":
          [
            {"property":[2,5],"type":2},
            {"property":[1,2],"type":1},
            {"property":[4,0],"type":0}
          ],
 "prop2":"something"
}

Currently the java looks like 目前的java看起来像

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Parent{
       <WHAT TO PUT HERE??>
       List<PropertyTypeObj> propertyTypes;
    }

This is part of something larger like: 这是更大的东西的一部分,例如:

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Container{

        @JsonProperty("parent")
        List<Parent> parent;
        @JsonProperty("prop2")
        String prop2
    }

The solution was to bypass the parent element creation and instead use the PropertyTypeObject itself 解决方案是绕过父元素的创建,而是使用PropertyTypeObject本身

@JsonInclude(JsonInclude.Include.NON_NULL)
        public class Container{

            @JsonProperty("parent")
            List<PropertyTypeObject> properties;
            @JsonProperty("prop2")
            String prop2
        }

And then specify the PropertyTypeObject as having @JsonRootName("parent") 然后将PropertyTypeObject指定为具有@JsonRootName("parent")

See approved answer for clarity. 请参阅批准的答案以了解清楚。

A possible class structure is the following: 可能的类结构如下:

public class External {
   private List<External.Internal> parent;
   private String prop2; 

   @JsonRootName("parent")
   public static class Internal {
     private List<Integer> property;
     private Integer type;
   }
}

where the external class has: 外部类具有:

  • a parent property that is a List (array in the json) of Inner elements 一个属性,它是内部元素的列表(json中的数组)
  • prop2 property of type String 类型为string的prop2属性

and an internal class that has for each element: 和一个内部类,每个元素具有:

  • a property property of type List (array in json) of integers 整数的List类型(json中的数组)的property属性
  • a type property of type integer 整数类型的类型属性

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

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