0

我有一个 json 字符串

{
"user": [
    {
        "actor": "ashok"
    },
    {
        "actor": {
            "name": "ashok",
            "mail": "ashok@gmail.com"
        }
    },
    {
        "actor": [
            "ashok",
            "kumar"
        ]
    }
]
}

如何生成 POJO 类。这是为了让杰克逊映射器自动映射 bean 类中的以下 json。

4

1 回答 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; 
      }
   }
}  

第二种情况,您可以创建带有两个字符串字段的简单类,name然后mail在其上创建新实例if (value instanceof Map)

于 2012-12-07T09:06:34.800 回答