---
title: "json-lib包笔记"
author: "weishuwei"
date: "2007-07-03T17:41:58Z"
tags: []
categories: ["企业架构"]
slug: "json-lib"
draft: false
lastmod: "2007-07-03T17:41:58Z"
id: 97384
url: "https://weishuwei.iteye.com/blog/97384"
---

json-lib.jar开发包使用:

**依赖包:**  
commons-beanutils.jar;  
commons-httpclient.jar;  
commons-lang.jar;  
ezmorph.jar;不少人使用时会提示net.sf.ezmorph.xxx找不到,就是缺这个:  
morph-1.0.1.jar

**相关链接:**  
[http://json-lib.sourceforge.net/](http://json-lib.sourceforge.net/)  
[http://ezmorph.sourceforge.net/](http://ezmorph.sourceforge.net/)  
[http://morph.sourceforge.net/](http://morph.sourceforge.net/)

**使用过程中问题:**  
1,把bean转化为json格式时老提示如下错误:  
Exception in thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: Property 'name' has no getter method  
解决:声明bean为public class xxx,必须是public,我用默认类型(class xxx)都不行

2,Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.lang.ArrayUtils.toObject([C)[Ljava/lang/Character;  
原因:定义属性如下:private char\[] options = new char\[] { 'a', 'f' };好像不能处理这种类型的

3, private String func1 = "function(i){ return this.options\[i]; }";  
   和  
   private JSONFunction func2 = new JSONFunction(new String\[] { "i" },  
     "return this.options\[i];");  
   转换后显示结果差不多:  
   {"func1":function(i){ return this.options\[i];,"func2":function(i){ return this.options\[i]; }}

测试类:

 

01. import java.util.ArrayList;
02. import java.util.HashMap;
03. import java.util.List;
04. import java.util.Map;
05. import net.sf.json.JSONArray;
06. import net.sf.json.JSONObject;
07. public class Json {
08. public static void main(String\[] args) {
09. Json j = new Json();
10. j.bean2json();
11. }
12. public void arr2json() {
13. boolean\[] boolArray = new boolean\[] { true, false, true };
14. JSONArray jsonArray = JSONArray.fromObject(boolArray);
15. System.out.println(jsonArray);
16. // prints \[true,false,true]
17. }
18. public void list2json() {
19. List list = new ArrayList();
20. list.add("first");
21. list.add("second");
22. JSONArray jsonArray = JSONArray.fromObject(list);
23. System.out.println(jsonArray);
24. // prints \["first","second"]
25. }
26. public void createJson() {
27. JSONArray jsonArray = JSONArray.fromObject("\['json','is','easy']");
28. System.out.println(jsonArray);
29. // prints \["json","is","easy"]
30. }
31. public void map2json() {
32. Map
33. map.put("name", "json");
34. map.put("bool", Boolean.TRUE);
35. map.put("int", new Integer(1));
36. map.put("arr", new String\[] { "a", "b" });
37. map.put("func", "function(i){ return this.arr\[i]; }");
38. JSONObject json = JSONObject.fromObject(map);
39. System.out.println(json);
40. // prints
41. // \["name":"json","bool":true,"int":1,"arr":\["a","b"],"func":function(i){
42. // return this.arr\[i]; }]
43. }
44. public void bean2json() {
45. JSONObject jsonObject = JSONObject.fromObject(new MyBean());
46. System.out.println(jsonObject);
47. /*
48. * prints
49. * {"func1":function(i){ return this.options\[i];
50. * },"pojoId":1,"name":"json","func2":function(i){ return
51. * this.options\[i]; }}
52. \*/
53. }
54. public void json2bean() {
55. String json = "{name=\\"json2\\",func1:true,pojoId:1,func2:function(a){ return a; },options:\['1','2']}";
56. JSONObject jb = JSONObject.fromString(json);
57. JSONObject.toBean(jb, MyBean.class);
58. System.out.println();
59. }
60. }

操作的bean:

 

01. import net.sf.json.JSONFunction;
02. public class MyBean {
03. private String name = "json";
04. private int pojoId = 1;
05. // private char\[] options = new char\[] { 'a', 'f' };
06. private String func1 = "function(i){ return this.options\[i]; }";
07. private JSONFunction func2 = new JSONFunction(new String\[] { "i" },
08. "return this.options\[i];");
09. // getters & setters
10. ......
11. }

**题外话:** **这个我对json-lib包的初次尝试,希望对大家有所帮助,另外大家有谁用过其它处理json的开发包,提出来,大家探讨一下~!!!!**