- 每次用到json的时候总得去查一下,所以小小的总结一下
- 官方:http://code.alibabatech.com/wiki/pages/viewpage.action?pageId=2424946
- 具体常用的转换代码:
(1)序列化
1.1 对象(User):
String text = JSON.toJSONString(user /*,SerializerFeature.UseSingleQuotes */);
1.2 list / map / set集合:
String text=JSON.toJSONString(users/*,SerializerFeature.UseSingleQuotes*/);
(2)反序列化
2.1 对象(User): eg: {"name":"愿望 ","stime":1377087488762}
User u=JSON.parSEObject(text,User.class);
2.2 list集合 eg: [{"name":"张三","stime":1377087570508},{"name":"李四","stime":1377087570508}]
List<User> list = JSON.parseArray(text,User.class);
2.3 map集合 eg: {1:{"name":"张三","stime":1377087637172},2:{"name":"李四","stime":1377087637172}}
Map<Long,User> userMap = JSON.parSEObject(text,new TypeReference<Map<Long,User>>() {});
2.4 set集合 eg: [{"name":"李四","stime":1377087674850},{"name":"张三","stime":1377087674850}]
Set<User> userMap = JSON.parSEObject(text,new TypeReference<Set<User>>(){});
- 附件几个testcase