简体   繁体   中英

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

@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

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

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
  • prop2 property of type String

and an internal class that has for each element:

  • a property property of type List (array in json) of integers
  • a type property of type integer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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