json的使用
json:的主要作用是进行数据转换,把复杂的结果用简单的方式描述,用于传输。
?
http://json-lib.sourceforge.net/usage.html
?
?
1)JSON简介?
2)JSON/LIST转换?
3)JSON/MAP转换?
4)JSON/动态Bean转换?
5)JSON/静态Bean转换?
6)JSON/XML输出?
1.JSON简介?
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,基于JavaScript,但是不仅仅限于此。?
详情可以参考www.json.org?
例如一段XML?
<?xml version="1.0" encoding="utf-8"?>?
<shop>?
<name>饭店</name>?
??? <city>北京</city>?
</shop>?
用JSON表示如下:?
{?
"name":"饭店",?
"city":"北京"?
}?
XML的解析得考虑子节点父节点关系,而JSON的解析难度相当低,很多控件,尤其是ajax相关的数据交换很多都用json.?
2)JSON/LIST转换?
本教程解析采用的json-lib,官方网站为http://json-lib.sourceforge.net/ ,本教程参考官方教程?
环境需要配置的jar如下?
commons-beanutils和ezmorph控制反射?
commons-collections是apachecommons的子项目,扩展了java集合类?
commons-lang扩展了java.lang包?
commons-logging日志类?
xom是xml解析类,可以参考www.xom.nu?
junit单元测试用的jar?
json-lib核心jar?
项目文件夹中拥有货物参数(Shop)和货物参数列表(ShopList)两个实体?
Shop包含name和property两个字段,ShopList包含Shop的列表?
对应的json是?
String s = "[{name:'重量',property:'p1'},{name:'尺寸',property:'p2'},{name:'显卡类型 ',property:'p3'},{name:'硬盘容量',property:'p4'},{name:'处理器',property:'p5'}, {name:'内存',property:'p6'},{name:'型号',property:'p7'},{name:'货号 ',property:'p8'},{name:'品牌',property:'p9'}]";?
把这样的数据结构作为用户定义个人信息存入数据库可以达到个性化参数的作用,?
比如shopex的数据库中很多表就是用的json数据类型。因为用户自己添加的参数的长度是不固定的?
比如上述例子就拥有9个用户自定义的参数?
当用户需要填写这些参数的时候,需要转化为list,然后在struts2的view去显示?
完成的代码可以参考附件的ArrayUtil文件?
核心代码仅仅就一行JSONArray jsonArray = JSONArray.fromObject(s);?
得到这个jsonArray后要转化为ArrayList,需要用循环遍历,如下?
for (int i = 0; i < jsonArray.size(); i++) {?
?? Object o = jsonArray.get(i);?
?? JSONObject jsonObject = JSONObject.fromObject(o);?
?? Shop Person = (Shop) JSONObject.toBean(jsonObject, Shop.class);?
?? list.add(Person);?
}?
然后得到的list就是普通的ArrayList了?
3)JSON/MAP转换?
当我们初始化完一个map,放入json可以直接放入?
Map<String, String> map = new HashMap<String, String>();?
map.put("name", "重量");?
map.put("property", "p1");?
JSONObject jsonObject = JSONObject.fromObject(map);?
核心代码为?
JSONObject jsonObject = JSONObject.fromObject(map);?
JsonLib会自动映射?
完成例子见附件MapUtil.java?
4)JSON/动态Bean转换?
所谓动态bean即是java运行的时候根据情况创建的,而不是程序员已经好了的Bean?
JsonLib会自动根据Json格式数据创建字段,然后创建一个包含这些字段的Object?
本例子中采用JUNIT做单元测试验证,见DynamicBean.java?
String s = "{name:'重量',property:'p1'}";?
JSONObject jsonObject = JSONObject.fromObject(s);?
Object bean = JSONObject.toBean(jsonObject);?
assertEquals(jsonObject.get("name"), PropertyUtils.getProperty(bean,"name"));?
assertEquals(jsonObject.get("property"), PropertyUtils.getProperty(bean,"property"));?
5)JSON/静态Bean转换(StaticBean.java)?
JSONLIB在转换的时候会自动查找关系,比如子类和父类?
例如JSON数据源?
String s = "{'shopList':[{name:'重量',property:'p1'},{name:'尺寸',property:'p2'}, {name:'显卡类型',property:'p3'},{name:'硬盘容量',property:'p4'},{name:'处理器 ',property:'p5'},{name:'内存',property:'p6'},{name:'型号',property:'p7'}, {name:'货号',property:'p8'},{name:'品牌',property:'p9'}]}";?
存入Map?
map.put("shopList", Shop.class);?
ShopList shopList = (ShopList) JSONObject.toBean(JSONObject.fromObject(s), ShopList.class, map);?
JSONObject.toBean()方法的三个参数分别表示数据源对应的JSON对象,转化后的对象ShopList和数据源map?
然后这样也可以取得ShopList?
这种方法和动态转换的区别在于,动态转换仅仅只是转为Object?
而静态转换是转换为已经定义过的实体类,会自动映射(这点类似Ibatis)?
6)JSON/XML输出?
如果自己用String的方法转化为XML输出要写很多代码,然后条用JSONLIB,核心代码仅仅一步?
String xmlObject = xmlSerializer.write(object);?
比如?
String s = "{name:'重量',property:'p1'}";?
XMLSerializer xmlSerializer = new XMLSerializer();?
JSONObject object = JSONObject.fromObject(s);?
String xmlObject = xmlSerializer.write(object);?
System.out.println(xmlObject);?
输出结果为?
<?xml version="1.0" encoding="UTF-8"?>?
<o>?
? <name type="string">重量</name>?
? <property type="string">p1</property>?
</o>?
json-lib 将list或者对象转化为json格式?
1? 示例说明:?
list -> json:?
Student[] stus = new Student[5];?
List<Student> stuList = new ArrayList<Student>();?
for (int i = 0; i < stus.length; i++) {?
stus[i] = new Student();?
stus[i].setAge(i*10+8);?
stus[i].setName("张三"+i);?
stus[i].setSex("和");?
//添加到list,一会儿用?
? stuList.add(stus[i]);?
}?
JSONArray jsonArray = JSONArray.fromObject(stus);?
System.out.println(jsonArray);?
对象(Student)-->json:?
Student student = new Student();?
student.setAge(18);?
student.setName("zhangsan");?
student.setSex("male");?
JSONObject jsonObject = JSONObject.fromObject(student);?
System.out.println(jsonObject);?
map+list?? --> json:?
Student[] stus = new Student[5];?
List<Student> stuList = new ArrayList<Student>();?
for (int i = 0; i < stus.length; i++) {?
stus[i] = new Student();?
stus[i].setAge(i * 10 +
;?
stus[i].setName("张三" + i);?
stus[i].setSex("和");?
// 添加到list,一会儿用?
? stuList.add(stus[i]);?
}?
Map<String, Object> map = new HashMap<String, Object>();?
Teacher teacher = new Teacher();?
teacher.setAge(30);?
teacher.setName("laoshi");?
teacher.setSex("male");?
map.put("teacher", teacher);?
map.put("student", stuList);?
JSONObject jsonObjectFromMap = JSONObject.fromObject(map);?
System.out.println(jsonObjectFromMap);?
?
?
?
?
?
?