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

CosminO :

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.

Davide Lorenzo MARINO :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=130000&siteId=1