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包有:
注意这里如果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; |
03 | import net.sf.json.JSONFunction; |
06 | * @author xudongwang 2012-1-15 |
08 | * Email:xdwangiflytek@gmail.com |
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];" ); |
19 | public String getName() { |
23 | public void setName(String name) { |
27 | public int getPojoId() { |
31 | public void setPojoId( int pojoId) { |
35 | public char [] getOptions() { |
39 | public void setOptions( char [] options) { |
40 | this .options = options; |
43 | public String getFunc1() { |
47 | public void setFunc1(String func1) { |
51 | public JSONFunction getFunc2() { |
55 | public void setFunc2(JSONFunction func2) { |
JsonlibTest.java
:
001 | package com.iflytek.json; |
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; |
009 | import net.sf.json.JSONArray; |
010 | import net.sf.json.JSONObject; |
012 | import org.apache.commons.beanutils.PropertyUtils; |
015 | * @author xudongwang 2012-1-15 |
017 | * Email:xdwangiflytek@gmail.com |
019 | public class JsonlibTest { |
022 | private JSONArray jsonArray = null ; |
024 | private JSONObject jsonObject = null ; |
026 | public static void main(String[] args) { |
027 | JsonlibTest json = new JsonlibTest(); |
038 | public void ArrayToJSON() { |
039 | boolean [] boolArray = new boolean [] { true , false , true }; |
040 | jsonArray = JSONArray.fromObject(boolArray); |
041 | System.out.println( "数组转JSON操作:" + jsonArray); |
047 | public void ListToJSON() { |
048 | List<String> list = new ArrayList<String>(); |
051 | jsonArray = JSONArray.fromObject(list); |
052 | System.out.println( "集合转JSON操作:" + jsonArray); |
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); |
072 | public void BeanToJSON() { |
073 | jsonObject = JSONObject.fromObject( new MyBean()); |
074 | System.out.println( "Bean转JSON操作:" + jsonObject); |
080 | public void JSONToBean() { |
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); |
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) { |
104 | } catch (InvocationTargetException e) { |
106 | } catch (NoSuchMethodException e) { |
一、简介
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项目当中,即可调用。
在进行下面的代码演示之前,我们先将几个基本的类介绍一下:
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 = { "身份证" , "银行卡" , "公车卡" }; |
08 | public String getId() { |
11 | public void setId(String id) { |
14 | public String getName() { |
17 | public void setName(String name) { |
20 | public Date getDate() { |
23 | public void setDate(Date date) { |
26 | public List getCardNum() { |
29 | public void setCardNum(List cardNum) { |
30 | this .cardNum = cardNum; |
32 | public String[] getCardType() { |
35 | public void setCardType(String[] cardType) { |
36 | this .cardType = cardType; |
01 | <span style= "font-size:14px;" > public class Person { |
02 | private String name = null ; |
05 | public Person(String name){ |
08 | public String getName() { |
11 | public void setName(String name) { |
17 | public class MyBeanWithPerson { |
18 | private List<Person> list = null ; |
19 | private Map<String,Person> map = null ; |
20 | public List getList() { |
23 | public void setList(List list) { |
29 | public void setMap(Map map) { |
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); |
01 | <span style= "font-size:14px;" > public static void testBeadToJSON() { |
02 | MyBean bean = new MyBean(); |
05 | bean.setDate( new Date()); |
07 | List cardNum = new ArrayList(); |
11 | cardNum.add( new Person( "test" )); |
13 | bean.setCardNum(cardNum); |
15 | JSONObject jsonObject = JSONObject.fromObject(bean); |
16 | System.out.println(jsonObject); |
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); |
1 | <span style= "font-size:14px;" > public static void testListToJSON() { |
2 | List list = new ArrayList(); |
5 | JSONArray jsonArray = JSONArray.fromObject(list); |
6 | System.out.println(jsonArray); |
7 | } </span><span style= "font-family:Simsun;font-size:14px;" > </span> |
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]; }" ); |
09 | JSONObject jsonObject = JSONObject.fromObject(map); |
10 | System.out.println(jsonObject); |
01 | <span style= "font-size:14px;" > public static void testJSONToObject() throws Exception { |
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); |
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); |
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); |
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()); |
8.将复合类型的JSON字符串转换成复合对象,包含Map
01 | <span style= "font-size:14px;" > public static void testJSONToBeanHavaMap() { |
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 ); |
08 | MyBeanWithPerson diyBean =(MyBeanWithPerson)JSONObject.toBean(JSONObject.fromObject(json), MyBeanWithPerson. class , classMap); |
09 | System.out.println(diyBean); |
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()); |
18 | System.out.println( "do the map release" ); |
20 | MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry(); |
21 | Morpher dynaMorpher = new BeanMorpher(Person. class , morpherRegistry); |
22 | morpherRegistry.registerMorpher(dynaMorpher); |
24 | Map map = diyBean.getMap(); |
26 | System.out.println(map); |
27 | ?List<Person> output = new ArrayList(); |
28 | for (Iterator i = map.values().iterator(); i.hasNext();) { |
30 | output.add((Person) morpherRegistry.morph(Person. class , i.next())); |
33 | for (Person p : output) { |
34 | System.out.println(p.getName()); |