Reputation: 195
I have a json String
{
"user": [
{
"actor": "ashok"
},
{
"actor": {
"name": "ashok",
"mail": "[email protected]"
}
},
{
"actor": [
"ashok",
"kumar"
]
}
]
}
How to generate a POJO class. This is for the jackson mapper to automatically map the following json in bean class.
Upvotes: 0
Views: 515
Reputation: 29693
public class MainBean
{
private List<UserBean> user = new ArrayList<UserBean>();
// getter/ setter
}
public class UserBean
{
private String actor; // this for "actor": "ashok"
private Map<String, String> actorMap; // this for second case
private List<String> actors; // this for third case
@JsonAnySetter
public void set(String name, Object value)
{
if (value instanceof String)
{
actor = (String) value;
}
else if (value instanceof Map)
{
actorMap = (Map<String, String>) value;
}
else if (value instanceof List)
{
actors = (List<String>) value;
}
}
}
is second case you can create simple class with two string fields name
and mail
and create new instance on it after if (value instanceof Map)
Upvotes: 1