Json对象与Java对象转换

Posted zhumengke

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Json对象与Java对象转换相关的知识,希望对你有一定的参考价值。

推荐网站:

http://www.runoob.com/json/json-stringify.html

http://www.w3school.com.cn/json/json_eval.asp

转自:http://www.jb51.net/article/120737.htm

首先,引入 json-lib包

1、Json串转化为List对象:

JSONArray jsonArray = JSONArray.fromObject(jsonString);
List<Config> list = (List) JSONArray.toCollection(jsonArray,Class.class); 

2、Json串转化为Object对象

JSONObject jsonObject = JSONObject.fromObject(jsonString);
Object object = (Object) JSONObject.toBean(jsonObject, Object.class);

3、Json串转化为包含List对象的Object对象

JSONObject jsonObject = JSONObject.fromObject(jsonString);
Map<String, Class> listMap = new HashMap<String, Class>();
listMap.put("list", listObject.class);
Object object = (Object) JSONObject.toBean(jsonObject, Object.class, listMap);

PS:Object对象中可能包含了多个对象,对象中可能又包含了多个List对象互相嵌套。
只需要把所有的List对象值组装成Map对象即可,对应的key为List对象的属性名称。



以上是关于Json对象与Java对象转换的主要内容,如果未能解决你的问题,请参考以下文章