Json_lib可以方便的将Java对象转成json格式的字符串,也可以将Java对象转换成xml格式的文档,同样可以将Json字符串转换成Java对象,或者将xml字符串转换成Java对象。
官网:http://json-lib.sourceforge.net/
JSON在线转换:http://json.parser.online.fr/
JSON教程:http://www.json.org/json-zh.html
官网上说明了json_lib还需要依赖的Jar包有:
JAR |
网址 |
http://commons.apache.org/collections/download_collections.cgi |
|
jakarta commons-logging 1.1.1 |
|
注意这里如果jakarta commons-lang 使用3.1版本的会报:Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
所以这里还是使用2.6的吧
MyBean.java:
01 |
package com.iflytek.json; |
02 |
03 |
import net.sf.json.JSONFunction; |
04 |
05 |
/** |
06 |
* @author xudongwang 2012-1-15 |
07 |
* |
08 |
* Email:[email protected] |
09 |
*/ |
10 |
public class MyBean { |
11 |
private String name = "json" ; |
12 |
private int pojoId = 1 ; |
13 |
private char [] options = new char [] { 'a' , 'f' }; |
14 |
private String func1 = "function(i){ return this.options[i]; }" ; |
15 |
private JSONFunction func2 = new JSONFunction( new String[] { "i" }, |
16 |
"return this.options[i];" ); |
17 |
|
18 |
|
19 |
public String getName() { |
20 |
return name; |
21 |
} |
22 |
23 |
public void setName(String name) { |
24 |
this .name = name; |
25 |
} |
26 |
27 |
public int getPojoId() { |
28 |
return pojoId; |
29 |
} |
30 |
31 |
public void setPojoId( int pojoId) { |
32 |
this .pojoId = pojoId; |
33 |
} |
34 |
35 |
public char [] getOptions() { |
36 |
return options; |
37 |
} |
38 |
39 |
public void setOptions( char [] options) { |
40 |
this .options = options; |
41 |
} |
42 |
43 |
public String getFunc1() { |
44 |
return func1; |
45 |
} |
46 |
47 |
public void setFunc1(String func1) { |
48 |
this .func1 = func1; |
49 |
} |
50 |
51 |
public JSONFunction getFunc2() { |
52 |
return func2; |
53 |
} |
54 |
55 |
public void setFunc2(JSONFunction func2) { |
56 |
this .func2 = func2; |
57 |
} |
58 |
59 |
} |
001 |
package com.iflytek.json; |
002 |
003 |
import java.lang.reflect.InvocationTargetException; |
004 |
import java.util.ArrayList; |
005 |
import java.util.HashMap; |
006 |
import java.util.List; |
007 |
import java.util.Map; |
008 |
009 |
import net.sf.json.JSONArray; |
010 |
import net.sf.json.JSONObject; |
011 |
012 |
import org.apache.commons.beanutils.PropertyUtils; |
013 |
014 |
/** |
015 |
* @author xudongwang 2012-1-15 |
016 |
* |
017 |
* Email:[email protected] |
018 |
*/ |
019 |
public class JsonlibTest { |
020 |
021 |
// JSONArray是将一个Java对象转换成json的Array格式,如['xdwang', 22] |
022 |
private JSONArray jsonArray = null ; |
023 |
// JSONObject是将Java对象转换成一个json的Object形式,如{name:'xdwang', age: 22} |
024 |
private JSONObject jsonObject = null ; |
025 |
026 |
public static void main(String[] args) { |
027 |
JsonlibTest json = new JsonlibTest(); |
028 |
json.ArrayToJSON(); |
029 |
json.ListToJSON(); |
030 |
json.MapsToJSON(); |
031 |
json.BeanToJSON(); |
032 |
json.JSONToBean(); |
033 |
} |
034 |
035 |
/** |
036 |
* 数组转JSON操作 |
037 |
*/ |
038 |
public void ArrayToJSON() { |
039 |
boolean [] boolArray = new boolean [] { true , false , true }; |
040 |
jsonArray = JSONArray.fromObject(boolArray); |
041 |
System.out.println( "数组转JSON操作:" + jsonArray); |
042 |
} |
043 |
044 |
/** |
045 |
* 集合转JSON操作 |
046 |
*/ |
047 |
public void ListToJSON() { |
048 |
List<String> list = new ArrayList<String>(); |
049 |
list.add( "first" ); |
050 |
list.add( "second" ); |
051 |
jsonArray = JSONArray.fromObject(list); |
052 |
System.out.println( "集合转JSON操作:" + jsonArray); |
053 |
} |
054 |
055 |
/** |
056 |
* Maps转JSON操作 |
057 |
*/ |
058 |
public void MapsToJSON() { |
059 |
Map<String, Object> map = new HashMap<String, Object>(); |
060 |
map.put( "name" , "json" ); |
061 |
map.put( "bool" , Boolean.TRUE); |
062 |
map.put( "int" , new Integer( 1 )); |
063 |
map.put( "arr" , new String[] { "a" , "b" }); |
064 |
map.put( "func" , "function(i){ return this.arr[i]; }" ); |
065 |
jsonObject = JSONObject.fromObject(map); |
066 |
System.out.println( "Maps转JSON操作:" + jsonObject); |
067 |
} |
068 |
069 |
/** |
070 |
* Bean转JSON操作 |
071 |
*/ |
072 |
public void BeanToJSON() { |
073 |
jsonObject = JSONObject.fromObject( new MyBean()); |
074 |
System.out.println( "Bean转JSON操作:" + jsonObject); |
075 |
} |
076 |
077 |
/** |
078 |
* JSON转Bean操作 |
079 |
*/ |
080 |
public void JSONToBean() { |
081 |
try { |
082 |
String json = "{\"func1\":function(i){ return this.options[i]; },\"func2\":function(i){ return this.options[i]; },\"name\":\"json\",\"options\":[\"a\",\"f\"],\"pojoId\":1}" ; |
083 |
jsonObject = JSONObject.fromObject(json); |
084 |
Object bean = JSONObject.toBean(jsonObject); |
085 |
System.out.println( "jsonStr:" + json); |
086 |
087 |
System.out.println( "name:" + jsonObject.get( "name" )); |
088 |
System.out.println( "name:" |
089 |
+ PropertyUtils.getProperty(bean, "name" )); |
090 |
System.out.println( "pojoId:" + jsonObject.get( "pojoId" )); |
091 |
System.out.println( "pojoId:" |
092 |
+ PropertyUtils.getProperty(bean, "pojoId" )); |
093 |
System.out.println( "options:" + jsonObject.get( "options" )); |
094 |
System.out.println( "options:" |
095 |
+ PropertyUtils.getProperty(bean, "options" )); |
096 |
System.out.println( "func1:" + jsonObject.get( "func1" )); |
097 |
System.out.println( "func1:" |
098 |
+ PropertyUtils.getProperty(bean, "func1" )); |
099 |
System.out.println( "func2:" + jsonObject.get( "func2" )); |
100 |
System.out.println( "func2:" |
101 |
+ PropertyUtils.getProperty(bean, "func2" )); |
102 |
} catch (IllegalAccessException e) { |
103 |
e.printStackTrace(); |
104 |
} catch (InvocationTargetException e) { |
105 |
e.printStackTrace(); |
106 |
} catch (NoSuchMethodException e) { |
107 |
e.printStackTrace(); |
108 |
} |
109 |
} |
110 |
111 |
} |
一、简介
json-lib是一个Java类库,提供将Java对象,包括beans,maps,collections,java arrays和xml等转换成JSON,或者反向转换的功能。
二、准备
在使用json-lib之前,我们应该到官方网址下载如下包:
-
jakarta commons-lang 2.5
-
jakarta commons-beanutils 1.8.0
-
jakarta commons-collections 3.2.1
-
jakarta commons-logging 1.1.1
-
ezmorph 1.0.6
并将这些jar包引入到Eclipse项目当中,即可调用。
三、讲解
在进行下面的代码演示之前,我们先将几个基本的类介绍一下:
MyBean类:
01 |
<span style= "font-size:14px;" > public class MyBean { |
02 |
private String id = null ; |
03 |
private String name = null ; |
04 |
private Date date = null ; |
05 |
private List cardNum = null ; |
06 |
private String[] cardType = { "身份证" , "银行卡" , "公车卡" }; |
07 |
|
08 |
public String getId() { |
09 |
return id; |
10 |
} |
11 |
public void setId(String id) { |
12 |
this .id = id; |
13 |
} |
14 |
public String getName() { |
15 |
return name; |
16 |
} |
17 |
public void setName(String name) { |
18 |
this .name = name; |
19 |
} |
20 |
public Date getDate() { |
21 |
return date; |
22 |
} |
23 |
public void setDate(Date date) { |
24 |
this .date = date; |
25 |
} |
26 |
public List getCardNum() { |
27 |
return cardNum; |
28 |
} |
29 |
public void setCardNum(List cardNum) { |
30 |
this .cardNum = cardNum; |
31 |
} |
32 |
public String[] getCardType() { |
33 |
return cardType; |
34 |
} |
35 |
public void setCardType(String[] cardType) { |
36 |
this .cardType = cardType; |
37 |
} |
38 |
} </span> |
Person类:
01 |
<span style= "font-size:14px;" > public class Person { |
02 |
private String name = null ; |
03 |
public Person(){ |
04 |
} |
05 |
public Person(String name){ |
06 |
this .name = name; |
07 |
} |
08 |
public String getName() { |
09 |
return name; |
10 |
} |
11 |
public void setName(String name) { |
12 |
this .name = name; |
13 |
} |
14 |
} |
15 |
|
16 |
MyBeanWithPerson类: |
17 |
public class MyBeanWithPerson { |
18 |
private List<Person> list = null ; |
19 |
private Map<String,Person> map = null ; |
20 |
public List getList() { |
21 |
return list; |
22 |
} |
23 |
public void setList(List list) { |
24 |
this .list = list; |
25 |
} |
26 |
public Map getMap() { |
27 |
return map; |
28 |
} |
29 |
public void setMap(Map map) { |
30 |
this .map = map; |
31 |
} |
32 |
} </span> |
1.将json字符串转换成JSON,根据情况用JSONArray或JSONObject
1 |
<span style= "font-size:14px;" > public static void testJsonStrToJSON() { |
2 |
JSONArray jsonArray = JSONArray.fromObject( "[\"json\",\"is\",\"easy\"]" ); |
3 |
System.out.println(jsonArray); |
4 |
} </span> |
2.将Java Bean转换成JSON对象
01 |
<span style= "font-size:14px;" > public static void testBeadToJSON() { |
02 |
MyBean bean = new MyBean(); |
03 |
bean.setId( "001" ); |
04 |
bean.setName( "银行卡" ); |
05 |
bean.setDate( new Date()); |
06 |
|
07 |
List cardNum = new ArrayList(); |
08 |
cardNum.add( "农行" ); |
09 |
cardNum.add( "工行" ); |
10 |
cardNum.add( "建行" ); |
11 |
cardNum.add( new Person( "test" )); |
12 |
|
13 |
bean.setCardNum(cardNum); |
14 |
|
15 |
JSONObject jsonObject = JSONObject.fromObject(bean); |
16 |
System.out.println(jsonObject); |
17 |
} </span> |
3.将一般的数组转换成JSON
1 |
<span style= "font-size:14px;" > public static void testArrayToJSON() { |
2 |
boolean [] boolArray = new boolean [] { true , false , true }; |
3 |
JSONArray jsonArray = JSONArray.fromObject(boolArray); |
4 |
System.out.println(jsonArray); |
5 |
} </span> |
4.将Collection对象转换成JSON
1 |
<span style= "font-size:14px;" > public static void testListToJSON() { |
2 |
List list = new ArrayList(); |
3 |
list.add( "first" ); |
4 |
list.add( "second" ); |
5 |
JSONArray jsonArray = JSONArray.fromObject(list); |
6 |
System.out.println(jsonArray); |
7 |
} </span><span style= "font-family:Simsun;font-size:14px;" > </span> |
5.将Map转换成JSON
01 |
<span style= "font-size:14px;" > public static void testMapToJSON() { |
02 |
Map map = new HashMap(); |
03 |
map.put( "name" , "json" ); |
04 |
map.put( "bool" , Boolean.TRUE); |
05 |
map.put( "int" , new Integer( 1 )); |
06 |
map.put( "arr" , new String[] { "a" , "b" }); |
07 |
map.put( "func" , "function(i){ return this.arr[i]; }" ); |
08 |
|
09 |
JSONObject jsonObject = JSONObject.fromObject(map); |
10 |
System.out.println(jsonObject); |
11 |
} </span> |
6.将普通类型的JSON字符串转换成JSON
01 |
<span style= "font-size:14px;" > public static void testJSONToObject() throws Exception { |
02 |
// 将JSon字符串转换成JsonObject对象 |
03 |
String json = "{name=\"json\",bool:true,int:1,double:2.2,func:\"function(a){ return a; }\",array:[1,2]}" ; |
04 |
JSONObject jsonObject = JSONObject.fromObject(json); |
05 |
System.out.println(jsonObject); |
06 |
|
07 |
// 将JsonObject对象转换成JavaBean对象 |
08 |
Object bean = JSONObject.toBean(jsonObject); |
09 |
System.out.println(PropertyUtils.getProperty(bean, "name" )); |
10 |
System.out.println(PropertyUtils.getProperty(bean, "bool" )); |
11 |
System.out.println(PropertyUtils.getProperty(bean, "int" )); |
12 |
System.out.println(PropertyUtils.getProperty(bean, "double" )); |
13 |
System.out.println(PropertyUtils.getProperty(bean, "func" )); |
14 |
System.out.println(PropertyUtils.getProperty(bean, "array" )); |
15 |
List arrayList = (List) JSONArray.toCollection(jsonObject.getJSONArray( "array" )); |
16 |
for (Object object : arrayList) { |
17 |
System.out.println(object); |
18 |
} |
19 |
} </span><span style= "font-family:Simsun;font-size:14px;" > </span> |
7.将复合类型的JSON字符串转换成复合对象,包含List
01 |
<span style= "font-size:14px;" > public static void testJSONToBeanHavaList() { |
02 |
String json = "{list:[{name:\"test1\"},{name:\"test2\"}]}" ; |
03 |
Map classMap = new HashMap(); |
04 |
classMap.put( "list" , Person. class ); |
05 |
MyBeanWithPerson diyBean = (MyBeanWithPerson) JSONObject.toBean( |
06 |
JSONObject.fromObject(json), MyBeanWithPerson. class , classMap); |
07 |
System.out.println(diyBean); |
08 |
|
09 |
List list = diyBean.getList(); |
10 |
for (Object o : list) { |
11 |
if (o instanceof Person) { |
12 |
Person p = (Person) o; |
13 |
System.out.println(p.getName()); |
14 |
} |
15 |
} |
16 |
} </span> |
8.将复合类型的JSON字符串转换成复合对象,包含Map
01 |
<span style= "font-size:14px;" > public static void testJSONToBeanHavaMap() { |
02 |
// 把Map看成一个对象 |
03 |
String json = "{list:[{name:\"test1\"},{name:\"test2\"}],map:{test1:{name:\"test1\"},test2:{name:\"test2\"}}}" ; |
04 |
Map classMap = new HashMap(); |
05 |
classMap.put( "list" , Person. class ); |
06 |
classMap.put( "map" , Map. class ); |
07 |
// 使用暗示,直接将json解析为指定自定义对象,其中List完全解析,Map没有完全解析 |
08 |
MyBeanWithPerson diyBean =(MyBeanWithPerson)JSONObject.toBean(JSONObject.fromObject(json), MyBeanWithPerson. class , classMap); |
09 |
System.out.println(diyBean); |
10 |
|
11 |
System.out.println( "do the list release" ); |
12 |
List<Person> list = diyBean.getList(); |
13 |
for (Person o : list) { |
14 |
Person p = (Person) o; |
15 |
System.out.println(p.getName()); |
16 |
} |
17 |
|
18 |
System.out.println( "do the map release" ); |
19 |
// 先往注册器中注册变换器,需要用到ezmorph包中的类 |
20 |
MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry(); |
21 |
Morpher dynaMorpher = new BeanMorpher(Person. class , morpherRegistry); |
22 |
morpherRegistry.registerMorpher(dynaMorpher); |
23 |
|
24 |
Map map = diyBean.getMap(); |
25 |
//这里的map没进行类型暗示,故按默认的,里面存的为net.sf.ezmorph.bean.MorphDynaBean类型的对象 |
26 |
System.out.println(map); |
27 |
?List<Person> output = new ArrayList(); |
28 |
for (Iterator i = map.values().iterator(); i.hasNext();) { |
29 |
// 使用注册器对指定DynaBean进行对象变换 |
30 |
output.add((Person) morpherRegistry.morph(Person. class , i.next())); |
31 |
} |
32 |
|
33 |
for (Person p : output) { |
34 |
System.out.println(p.getName()); |
35 |
} |
36 |
} </span> |