复杂JSON反序列化为类对象
有3种常⽤的反序列化库,gson和fastjson都很棒,json-lib有很⼤的局限性不推荐使⽤!
1. net.sf.json(json-lib)
只能⽤于解析简单的JSON,稍微复杂点的例如,类⾥⾯有含有List属性,这个没有问题(在0.9这个版本不⾏,但2.3可以,应该是bug修复了),但是List属性中类中再含有List就不
⽀持了,⾔外之意,类中含有List只能⼀层,再深就会报如下错误:
net.sf.json.JSONException: java.lang.NoSuchMethodException: Unknown property
代码如下:
Object object = ForObject(interfaceUrl, Object.class);
net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(object);
Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("productInfoList", ProductInfoList.class);
CobraProductInfoListDto j = (CobraProductInfoListDto) net.sf.Bean(json,
CobraProductInfoListDto.class, classMap);
2.gson
String interfaceUrl = ValueByKey(Constant.Product.interfaceUrl);
RestTemplate restTemplate = new RestTemplate();
Object object = ForObject(interfaceUrl, Object.class);
Gson gs= new Gson();
String json = gs.toJson(object);
CobraProductInfoListDto j = gs.fromJson(json, CobraProductInfoListDto.class);
System.out.ProductInfoList().get(0).getPLogo().getLogoName());
3.fastjson
String interfaceUrl = ValueByKey(Constant.Product.interfaceUrl);
RestTemplate restTemplate = new RestTemplate();object to
Object object = ForObject(interfaceUrl, Object.class);
Gson gs = new Gson();
String json = gs.toJson(object);
CobraProductInfoListDto j =JSON.parseObject(json, CobraProductInfoListDto.class);
String rateType = j.getProductInfoList().get(1).getPInterest().getCurrency();
另外推荐⼀个⾮常棒的根据Json⽣成Java/C#实体类的⼯具:tool.chinaz/tools/json2entity.aspx,很是⽅便!但是注意⽣成的代码可能需要优化,⽐如有些重复的类可
以合为⼀个,数据类型上需要把DateTime改为String,因为转换时存在数据的特例,可能某些字段需要由int改为double
在反序列化时,在如何定义类⽂件⽅式上,如下2种都可以,根据实际情况选取:
CobraProductInfoListDto j = JSON.parseObject(json, CobraProductInfoListDto.class);  //优势:访问⽅便,所有的全封装在⼀个类中,缺点是可能会多定义⼀些类
Map<String, List<ProductInfoList>> k = JSON.parseObject(json,
new TypeReference<Map<String, List<ProductInfoList>>>() {
}); //优势:和上⾯的⽅式⽐较起来可以少定义⼀些类⽂件
--------------------------------------------------------------
Json转换的时候切记:收尾不能包含引号,否则会报错,例⼦:
正确: {"name":"yuanyuan","description":"Randy chinese name"}
错误: "{"name":"yuanyuan","description":"Randy chinese name"}"
JSON字符串前⾯⽤引号包含,fastjson/gson所报错误的区别,感觉阿⾥的更易懂,google也不不能算错,但是理解起来费劲
com.alibaba.fastjson.JSONException: syntax error, expect {, actual string, pos 0
截取字符串收尾字符,例如去引号:.substring(1,json.length()-1 )
转义⼯具类:从StringEscapeUtils.escapeJava/unescapeJava
-----------------------------------------
发现google的gson在转换⽇期类型的时候会报错,但⽤阿⾥的fastjson就没有问题
Failed to parse date ["1454032939000']: Invalid time zone indicator '3'
期待的结果是://CST是中国标准时
Fri Jan 29 10:02:19 CST 2016
---------------------------------------------
反序列化json报错如下:
com.alibaba.fastjson.JSONException: illegal identifier : \pos 1, json :
{\"appStatus\":\"MADELOAN|MADELOANCOMPLETED\",\"category\":\"CREDIT_LOAN\",\"changedTime\":1454032939000,\"cobraAppStatus\":\"MADELOAN\",\"createdTime\":1454032581000,\"id\":6解决办法是把下⾯多余的转义字符给去掉:StringEscapeUtils.unescapeJava(String str)
{\"appStatus\":\"MADELOAN|MADELOANCOMPLETED\",\"category\":\"CREDIT_LOAN\",\"changedTime\":1454032939000,\"cobraAppStatus\":\"MADELOAN\",\"createdTime\":1454032581000,\"id\":6