JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把JSON转回成bean和DynaBean。
下载地址:
http://json-lib.sourceforge.net/
还要需要的第3方包:
org.apache.commons(3.2以上版本)
org.apache.oro
net.sf.ezmorph(ezmorph-1.0.4.jar)
nu.xom
1、List
- boolean[]boolArray=newboolean[]{true,false,true};
- JSONArrayjsonArray1=JSONArray.fromObject(boolArray);
- System.out.println(jsonArray1);
- Listlist=newArrayList();
- list.add("first");
- list.add("second");
- JSONArrayjsonArray2=JSONArray.fromObject(list);
- System.out.println(jsonArray2);
- JSONArrayjsonArray3=JSONArray.fromObject("['json','is','easy']");
- System.out.println(jsonArray3);
- boolean[]boolArray=newboolean[]{true,"easy"]
2、Map
- Mapmap=newHashMap();
- map.put("name","json");
- map.put("bool",Boolean.TRUE);
- map.put("int",newInteger(1));
- map.put("arr",newString[]{"a","b"});
- map.put("func","function(i){returnthis.arr[i];}");
- JSONObjectjson=JSONObject.fromObject(map);
- System.out.println(json);
- Mapmap=newHashMap();
- map.put("name","bool":true}
3、BEAN
- JSONObjectjsonObject=JSONObject.fromObject(newJsonBean());
- System.out.println(jsonObject);
4、BEANS
- Listlist=newArrayList();
- JsonBean2jb1=newJsonBean2();
- jb1.setCol(1);
- jb1.setRow(1);
- jb1.setValue("xx");
- JsonBean2jb2=newJsonBean2();
- jb2.setCol(2);
- jb2.setRow(2);
- jb2.setValue("");
- list.add(jb1);
- list.add(jb2);
- JSONArrayja=JSONArray.fromObject(list);
- System.out.println(ja.toString());
- Listlist=newArrayList();
- JsonBean2jb1=newJsonBean2();
- jb1.setCol(1);
- jb1.setRow(1);
- jb1.setValue("xx");
- JsonBean2jb2=newJsonBean2();
- jb2.setCol(2);
- jb2.setRow(2);
- jb2.setValue("");
- list.add(jb1);
- list.add(jb2);
- JSONArrayja=JSONArray.fromObject(list);
- System.out.println(ja.toString());
5、String to bean
- Stringjson="{name="json",bool:true,int:1,double:2.2,func:function(a){returna;},array:[1,2]}";
- JSONObjectjsonObject=JSONObject.fromString(json);
- Objectbean=JSONObject.toBean(jsonObject);
- assertEquals(jsonObject.get("name"),PropertyUtils.getProperty(bean,"name"));
- assertEquals(jsonObject.get("bool"),"bool"));
- assertEquals(jsonObject.get("int"),"int"));
- assertEquals(jsonObject.get("double"),"double"));
- assertEquals(jsonObject.get("func"),"func"));
- Listexpected=JSONArray.toList(jsonObject.getJSONArray("array"));
- assertEquals(expected,(List)PropertyUtils.getProperty(bean,"array"));
- Stringjson="{name="json","array"));
- Stringjson="{"value":"xx","row":1,"col":1}";
- JSONObjectjsonObject=JSONObject.fromString(json);
- JsonBean2bean=(JsonBean2)JSONObject.toBean(jsonObject,JsonBean2.class);
- assertEquals(jsonObject.get("col"),newInteger(bean.getCol()));
- assertEquals(jsonObject.get("row"),newInteger(bean.getRow()));
- assertEquals(jsonObject.get("value"),bean.getValue());
- Stringjson="{"value":"xx",bean.getValue());
6 json to xml 1) JSONObject json = new JSONObject( true ); String xml = XMLSerializer.write( json ); <o class="object" null="true"> 2) JSONObject json = JSONObject.fromObject("{"name":"json","bool":true,"int":1}"); String xml = XMLSerializer.write( json ); <o class="object"> <name type="string">json</name> <bool type="boolean">true</bool> <int type="number">1</int> </o> <o class="object"> <name type="string">json</name> <bool type="boolean">true</bool> <int type="number">1</int> </o> 3) JSONArray json = JSONArray.fromObject("[1,2,3]"); String xml = XMLSerializer.write( json ); <a class="array"> <e type="number">1</e> <e type="number">2</e> <e type="number">3</e> </a> 7 、xml to json <a class="array"> <e type="function" params="i,j"> return matrix[i][j]; </e> </a> <a class="array"> <e type="function" params="i,j"> return matrix[i][j]; </e> </a> JSONArray json = (JSONArray) XMLSerializer.read( xml ); System.out.println( json ); // prints [function(i,j){ return matrix[i][j]; }] (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|