1 package com.iflytek.json;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import net.sf.json.JSONArray;
10 import net.sf.json.JSONObject;
11
12 import org.apache.commons.beanutils.PropertyUtils;
13
14 /**
15 * @author xudongwang 2012-1-15
16 *
17 * Email:[email protected]
18 */
19 public class JsonlibTest {
20
21 // JSONArray是将一个Java对象转换成json的Array格式,如[‘xdwang‘, 22]
22 private JSONArray jsonArray = null;
23 // JSONObject是将Java对象转换成一个json的Object形式,如{name:‘xdwang‘, age: 22}
24 private JSONObject jsonObject = null;
25
26 public static void main(String[] args) {
27 JsonlibTest json = new JsonlibTest();
28 json.ArrayToJSON();
29 json.ListToJSON();
30 json.MapsToJSON();
31 json.BeanToJSON();
32 json.JSONToBean();
33 }
34
35 /**
36 * 数组转JSON操作
37 */
38 public void ArrayToJSON() {
39 boolean[] boolArray = new boolean[] { true, false, true };
40 jsonArray = JSONArray.fromObject(boolArray);
41 System.out.println("数组转JSON操作:" + jsonArray);
42 }
43
44 /**
45 * 集合转JSON操作
46 */
47 public void ListToJSON() {
48 List<String> list = new ArrayList<String>();
49 list.add("first");
50 list.add("second");
51 jsonArray = JSONArray.fromObject(list);
52 System.out.println("集合转JSON操作:" + jsonArray);
53 }
54
55 /**
56 * Maps转JSON操作
57 */
58 public void MapsToJSON() {
59 Map<String, Object> map = new HashMap<String, Object>();
60 map.put("name", "json");
61 map.put("bool", Boolean.TRUE);
62 map.put("int", new Integer(1));
63 map.put("arr", new String[] { "a", "b" });
64 map.put("func", "function(i){ return this.arr[i]; }");
65 jsonObject = JSONObject.fromObject(map);
66 System.out.println("Maps转JSON操作:" + jsonObject);
67 }
68
69 /**
70 * Bean转JSON操作
71 */
72 public void BeanToJSON() {
73 jsonObject = JSONObject.fromObject(new MyBean());
74 System.out.println("Bean转JSON操作:" + jsonObject);
75 }
76
77 /**
78 * JSON转Bean操作
79 */
80 public void JSONToBean() {
81 try {
82 String json = "{\"func1\":function(i){ return this.options[i]; },\"func2\":function(i){ return this.options[i]; },\"name\":\"json\",\"options\":[\"a\",\"f\"],\"pojoId\":1}";
83 jsonObject = JSONObject.fromObject(json);
84 Object bean = JSONObject.toBean(jsonObject);
85 System.out.println("jsonStr:" + json);
86
87 System.out.println("name:" + jsonObject.get("name"));
88 System.out.println("name:"
89 + PropertyUtils.getProperty(bean, "name"));
90 System.out.println("pojoId:" + jsonObject.get("pojoId"));
91 System.out.println("pojoId:"
92 + PropertyUtils.getProperty(bean, "pojoId"));
93 System.out.println("options:" + jsonObject.get("options"));
94 System.out.println("options:"
95 + PropertyUtils.getProperty(bean, "options"));
96 System.out.println("func1:" + jsonObject.get("func1"));
97 System.out.println("func1:"
98 + PropertyUtils.getProperty(bean, "func1"));
99 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 }