JSON与JAVA的数据转换

本文介绍如何使用JSON-lib这个Java类包实现Java的数据转换。通过此类包可以把bean,map和XML转换成JSON并能够把JSON转回成bean和DynaBean。

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);

//prints[true,false,true]

Listlist=newArrayList();

list.add("first");

list.add("second");

JSONArrayjsonArray2=JSONArray.fromObject(list);

System.out.println(jsonArray2);

/prints["first","second"]

JSONArrayjsonArray3=JSONArray.fromObject("['json','is','easy']");

System.out.println(jsonArray3);

//prints["json","is","easy"]

boolean[]boolArray=newboolean[]{true,false,true};

JSONArrayjsonArray1=JSONArray.fromObject(boolArray);

System.out.println(jsonArray1);

//prints[true,false,true]

Listlist=newArrayList();

list.add("first");

list.add("second");

JSONArrayjsonArray2=JSONArray.fromObject(list);

System.out.println(jsonArray2);

//prints["first","second"]

JSONArrayjsonArray3=JSONArray.fromObject("['json','is','easy']");

System.out.println(jsonArray3);

//prints["json","is","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);

//{"func":function(i){returnthis.arr[i];},"arr":["a","b"],"int":1,"name":"json","bool":true}

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);

//{"func":function(i){returnthis.arr[i];},"arr":["a","b"],"int":1,"name":"json","bool":true}

3、BEAN

JSONObjectjsonObject=JSONObject.fromObject(newJsonBean());

System.out.println(jsonObject);

//{"func1":function(i){returnthis.options[i];},"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){returnthis.options[i];}}

JSONObjectjsonObject=JSONObject.fromObject(newJsonBean());

System.out.println(jsonObject);

//{"func1":function(i){returnthis.options[i];},"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){returnthis.options[i];}}

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());

//[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]

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());

//[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]

5、Stringtobean

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"),PropertyUtils.getProperty(bean,"bool"));

assertEquals(jsonObject.get("int"),PropertyUtils.getProperty(bean,"int"));

assertEquals(jsonObject.get("double"),PropertyUtils.getProperty(bean,"double"));

assertEquals(jsonObject.get("func"),PropertyUtils.getProperty(bean,"func"));

Listexpected=JSONArray.toList(jsonObject.getJSONArray("array"));

assertEquals(expected,(List)PropertyUtils.getProperty(bean,"array"));

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"),PropertyUtils.getProperty(bean,"bool"));

assertEquals(jsonObject.get("int"),PropertyUtils.getProperty(bean,"int"));

assertEquals(jsonObject.get("double"),PropertyUtils.getProperty(bean,"double"));

assertEquals(jsonObject.get("func"),PropertyUtils.getProperty(bean,"func"));

Listexpected=JSONArray.toList(jsonObject.getJSONArray("array"));

assertEquals(expected,(List)PropertyUtils.getProperty(bean,"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\",\"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());

6jsontoxml

1)

JSONObjectjson=newJSONObject(true);

Stringxml=XMLSerializer.write(json);

<oclass="object"null="true">

2)

JSONObjectjson=JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");

Stringxml=XMLSerializer.write(json);

<oclass="object">

<nametype="string">json</name>

<booltype="boolean">true</bool>

<inttype="number">1</int>

</o>

<oclass="object">

<nametype="string">json</name>

<booltype="boolean">true</bool>

<inttype="number">1</int>

</o>

3)

JSONArrayjson=JSONArray.fromObject("[1,2,3]");

Stringxml=XMLSerializer.write(json);

<aclass="array">

<etype="number">1</e>

<etype="number">2</e>

<etype="number">3</e>

</a>

7、xmltojson

<aclass="array">

<etype="function"params="i,j">

returnmatrix[i][j];

</e>

</a>

<aclass="array">

<etype="function"params="i,j">

returnmatrix[i][j];

</e>

</a>

JSONArrayjson=(JSONArray)XMLSerializer.read(xml);

System.out.println(json);

//prints[function(i,j){returnmatrix[i][j];}]

相关推荐