0

Using Jackson how do I parse the following JSON:

{
    "foos" : [
        "one" : {
            "prop1" : "11",
            "prop2" : "11"
        },
        "two" : {
            "prop1" : "21",
            "prop2" : "21"
        },
        "three",
        "four"
    ]
}

Into these classes:

public class Root {
    private Set<Foo> foos;
    // ... getter and setter
}
public class Foo {
    private String name; // this should contain the JSON name, e.g.: "one", "two", "three" 
    private String prop1;
    private String prop2;
    // ... getters and setters
}

Regarding the JSON, I prefer to have named objects rather than:

"foos" : [
    {
        "name" : "one",
        "prop1" : "11",
        "prop2" : "11"
    },
    {
        "name" : "two",
        "prop1" : "21",
        "prop2" : "21"
    },
    {
        "name" : "three"
    },
    {
        "name" : "four"
    }
]

Because most of the foos contain no other properties, I don't know if the first JSON is less correct but it is more succinct.

Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56
pablo
  • 747
  • 1
  • 10
  • 24

1 Answers1

1

In this scenario, Map would fit in the purpose

public class Root {
    private Set<Foo> foos;
    // ... getter and setter
}

public class Foo {
    Map<String, InternalFooProps> map = new HashMap<>();    
    class InternalFooProps{
        private String prop1;
        private String prop2;
        // ... getters and setters
    }
}
Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56