wulfgarpro
wulfgarpro

Reputation: 6934

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 Jackson but can't find a suitable example.

Upvotes: 0

Views: 3443

Answers (2)

StaxMan
StaxMan

Reputation: 116502

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.

Upvotes: 1

Brian Roach
Brian Roach

Reputation: 76898

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

Upvotes: 3

Related Questions