0

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.

2
  • but it is valid there is only one mistack that is "actor":"ashok" in users array first elemnt Commented Dec 7, 2012 at 8:55
  • i correct the json string so now you can check Commented Dec 7, 2012 at 8:56

1 Answer 1

1
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)

Sign up to request clarification or add additional context in comments.

1 Comment

my main problem is given on this link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.