Using Jackson to parse JSON and extract embedded type
Say I have a compound JSON object like so:
{
"Person": {
"name":"test",
"age": 20
},
"Animal": {
"name":"Max"
}
}
This JSON representation has two embedded types Person
and Animal
, yet, I want to parse and extract a representation of each individual type (resulting in two Strings?).
Is this possible? I was thinking of using Jacks开发者_如何学JAVAon but can't find a suitable example.
Any JSON parser can do this.
If you're not looking to map to a POJO and want to use Jackson, you're probably looking for the Tree Model: http://wiki.fasterxml.com/JacksonTreeModel
It depends on exact details, but if you just mean that you have 2 different properties, with different types, you could have classes like:
public class Response {
public Person Person;
public Animal Animal;
}
public class Person {
public String name;
public int age;
}
public class Animal {
public String name;
}
(and/or use setters, getters).
But if you are looking for polymorphic types (types Person and animal being related), it requires more work.
精彩评论