将json String解析为Entity类

时间:2015-03-11 04:57:04

标签: java

{"field": {"11815": {"name": "email","value": "snow.fx@qq.com"},"11816": {"name": "storeid","value": "11"},"12116": {"name": "idcardtypeid","value": "1"},"12117": {"name": "idcardno","value": "320282199207071967"}},"birthday": "1992-7-7","name": "cccc","gender": 2,"openid": "90738","mobile": "1000"}

这是json字符串,我想将它解析为实体类,定义如下:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Userinfo(){String name; int gender; .... ; JSONObject field}

我使用ObjectMapper进行解析,代码为:

Userinfo userinfo = new ObjectMapper().readValue(jsonStr ,Userinfo.class)

但它仍会显示错误消息'无法识别的字段" 11216",未标记为我可忽略的'。

"生日","名称","移动"," openid",易于解决。 但" field"已经" 11815"," 11816"," 12116"," 12117",我无法定义名为&#34的变量; 11815",它在Java中是非法的

2 个答案:

答案 0 :(得分:0)

使用表示键名称的字段名称创建一个类,并使用提取的值初始化字段创建对象。如果您有多条记录,则可以创建对象列表。

答案 1 :(得分:0)

这是我的实体类

public class userinfo(){
    String name; 
    int gender; ...; 
    Map<String, Map<String, Object>> field;
}

所有变量都是'私有'类型,getter / setter是必不可少的。