Ashok kumar
Ashok kumar

Reputation: 195

How to generate pojo class for following json String

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

Answers (1)

Ilya
Ilya

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

Related Questions