java jsonlib 使用_使用json-lib进行Java和JSON之间的转换

1 packagecom.mai.json;2

3 import staticorg.junit.Assert.assertEquals;4

5 importjava.util.ArrayList;6 importjava.util.Date;7 importjava.util.HashMap;8 importjava.util.Iterator;9 importjava.util.List;10 importjava.util.Map;11 importnet.sf.ezmorph.Morpher;12 importnet.sf.ezmorph.MorpherRegistry;13 importnet.sf.ezmorph.bean.BeanMorpher;14 importnet.sf.json.JSONArray;15 importnet.sf.json.JSONObject;16 importnet.sf.json.util.JSONUtils;17

18 importorg.apache.commons.beanutils.PropertyUtils;19 importorg.junit.Test;20

21 public classJsonLibTest {22

23 /*

24 * 普通类型、List、Collection等都是用JSONArray解析25 *26 * Map、自定义类型是用JSONObject解析27 * 可以将Map理解成一个对象,里面的key/value对可以理解成对象的属性/属性值28 * 即{key1:value1,key2,value2......}29 *30 * 1.JSONObject是一个name:values集合,通过它的get(key)方法取得的是key后对应的value部分(字符串)31 * 通过它的getJSONObject(key)可以取到一个JSONObject,--> 转换成map,32 * 通过它的getJSONArray(key) 可以取到一个JSONArray ,33 *34 *35 */

36

37 //一般数组转换成JSON

38 @Test39 public voidtestArrayToJSON(){40 boolean[] boolArray = new boolean[]{true,false,true};41 JSONArray jsonArray =JSONArray.fromObject( boolArray );42 System.out.println( jsonArray );43 //prints [true,false,true]

44 }45

46

47 //Collection对象转换成JSON

48 @Test49 public voidtestListToJSON(){50 List list = newArrayList();51 list.add( "first");52 list.add( "second");53 JSONArray jsonArray =JSONArray.fromObject( list );54 System.out.println( jsonArray );55 //prints ["first","second"]

56 }57

58

59 //字符串json转换成json, 根据情况是用JSONArray或JSONObject

60 @Test61 public voidtestJsonStrToJSON(){62 JSONArray jsonArray = JSONArray.fromObject( "['json','is','easy']");63 System.out.println( jsonArray );64 //prints ["json","is","easy"]

65 }66

67

68 //Map转换成json, 是用jsonObject

69 @Test70 public voidtestMapToJSON(){71 Map map = newHashMap();72 map.put( "name", "json");73 map.put( "bool", Boolean.TRUE );74 map.put( "int", new Integer(1) );75 map.put( "arr", new String[]{"a","b"} );76 map.put( "func", "function(i){ return this.arr[i]; }");77

78 JSONObject jsonObject =JSONObject.fromObject( map );79 System.out.println( jsonObject );80 }81

82 //复合类型bean转成成json

83 @Test84 public voidtestBeadToJSON(){85 MyBean bean = newMyBean();86 bean.setId("001");87 bean.setName("银行卡");88 bean.setDate(newDate());89

90 List cardNum = newArrayList();91 cardNum.add("农行");92 cardNum.add("工行");93 cardNum.add("建行");94 cardNum.add(new Person("test"));95

96 bean.setCardNum(cardNum);97

98 JSONObject jsonObject =JSONObject.fromObject(bean);99 System.out.println(jsonObject);100

101 }102

103 //普通类型的json转换成对象

104 @Test105 public void testJSONToObject() throwsException{106 String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";107 JSONObject jsonObject =JSONObject.fromObject( json );108 System.out.println(jsonObject);109 Object bean =JSONObject.toBean( jsonObject );110 assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name") );111 assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool") );112 assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int") );113 assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double") );114 assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func") );115 System.out.println(PropertyUtils.getProperty(bean, "name"));116 System.out.println(PropertyUtils.getProperty(bean, "bool"));117 System.out.println(PropertyUtils.getProperty(bean, "int"));118 System.out.println(PropertyUtils.getProperty(bean, "double"));119 System.out.println(PropertyUtils.getProperty(bean, "func"));120 System.out.println(PropertyUtils.getProperty(bean, "array"));121

122 List arrayList = (List)JSONArray.toCollection(jsonObject.getJSONArray("array"));123 for(Object object : arrayList){124 System.out.println(object);125 }126

127 }128

129

130 //将json解析成复合类型对象, 包含List

131 @Test132 public voidtestJSONToBeanHavaList(){133 String json = "{list:[{name:'test1'},{name:'test2'}],map:{test1:{name:'test1'},test2:{name:'test2'}}}";134 //String json = "{list:[{name:'test1'},{name:'test2'}]}";

135 Map classMap = newHashMap();136 classMap.put("list", Person.class);137 MyBeanWithPerson diyBean = (MyBeanWithPerson)JSONObject.toBean(JSONObject.fromObject(json),MyBeanWithPerson.class, classMap);138 System.out.println(diyBean);139

140 List list =diyBean.getList();141 for(Object o : list){142 if(o instanceofPerson){143 Person p =(Person)o;144 System.out.println(p.getName());145 }146 }147 }148

149

150 //将json解析成复合类型对象, 包含Map

151 @Test152 public voidtestJSONToBeanHavaMap(){153 //把Map看成一个对象

154 String json = "{list:[{name:'test1'},{name:'test2'}],map:{testOne:{name:'test1'},testTwo:{name:'test2'}}}";155 Map classMap = newHashMap();156 classMap.put("list", Person.class);157 classMap.put("map", Map.class);158 //使用暗示,直接将json解析为指定自定义对象,其中List完全解析,Map没有完全解析

159 MyBeanWithPerson diyBean = (MyBeanWithPerson)JSONObject.toBean(JSONObject.fromObject(json),MyBeanWithPerson.class, classMap);160 System.out.println(diyBean);161

162 System.out.println("do the list release");163 List list =diyBean.getList();164 for(Person o : list){165 Person p =(Person)o;166 System.out.println(p.getName());167 }168

169 System.out.println("do the map release");170

171 //先往注册器中注册变换器,需要用到ezmorph包中的类

172 MorpherRegistry morpherRegistry =JSONUtils.getMorpherRegistry();173 Morpher dynaMorpher = new BeanMorpher( Person.class, morpherRegistry);174 morpherRegistry.registerMorpher( dynaMorpher );175

176

177 Map map =diyBean.getMap();178 /*这里的map没进行类型暗示,故按默认的,里面存的为net.sf.ezmorph.bean.MorphDynaBean类型的对象*/

179 System.out.println(map);180       /*输出:181 {testOne=net.sf.ezmorph.bean.MorphDynaBean@f73c1[182 {name=test1}183 ], testTwo=net.sf.ezmorph.bean.MorphDynaBean@186c6b2[184 {name=test2}185 ]}186       */

187 List output = newArrayList();188 for( Iterator i =map.values().iterator(); i.hasNext(); ){189 //使用注册器对指定DynaBean进行对象变换

190 output.add( (Person)morpherRegistry.morph( Person.class, i.next() ) );191 }192

193 for(Person p : output){194 System.out.println(p.getName());195         /*输出:196 test1197 test2198         */

199 }200

201 }202

203

204

205 }206

207 5.下面提供上面例子所需的资源,包括jar包和代码208

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值