When I have a simple json it is easy, take
{"type":"simple","content":"i love apples"}
I just create a pojo:
public class Example {
public Object type;
public Object content;
}
Then doing:
ObjectMapper mapper = new ObjectMapper();
Example ex = mapper.readValue(getInputStream(),Example.class)
will do the job.
But now suppose I have something more complicated, a multilevel json
{
"type": "complicated",
"params": [
{
"type": "simple",
"content": "i still love apples"
},
{
"type": "simple",
"content":"i love spam too"
}
]
}
As you can see the "params" field of this new Object is a json array, and each element of this array could be mapped to my Example
pojo class.
Is there a way to do this? Sorry if it could seem trivial, but I can't find any good documentation about jackson... it just talks about simple cases.
Here you go!
NOTE: no setters in the class, which is why I have to use
@JsonCreator
. Habit of mine, I don't do beans ;)If you have setters for the different fields you can do without
@JsonCreator
at all.