ITeye博客

json-lib包笔记

weishuwei · 2007-07-03 17:41 · jee框架模版 · 企业架构 · Markdown

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://ezmorph.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]; }}

测试类:

 

  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import net.sf.json.JSONArray;
  6. import net.sf.json.JSONObject;
  7. public class Json {
  8. public static void main(String[] args) {
  9. 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:

 

  1. import net.sf.json.JSONFunction;
  2. public class MyBean {
  3. private String name = "json";
  4. private int pojoId = 1;
  5. // private char[] options = new char[] { 'a', 'f' };
  6. private String func1 = "function(i){ return this.options[i]; }";
  7. private JSONFunction func2 = new JSONFunction(new String[] { "i" },
  8. "return this.options[i];");
  9. // getters & setters
  10. ......
  11. }

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

评论(8)

  1. #8 楼 · mlin · 2011-08-16 11:33

    不错不错,今天我也遇到了上面提到的第一个问题,看到你的文章才解决了,多谢多谢。

  2. #7 楼 · laiseeme · 2008-06-13 22:45

    andrew.yulong writes:

    我如果体格bean里面包含一个set集合,转换json的时候会报错,应该怎么解决》

    改写代码吧
    一般我反而是遇到返回值是set的就不生成json 因为是一对多关系经常不用加载

  3. #6 楼 · laiseeme · 2008-06-13 22:43

    什么时候讲讲和hibernate结合自动生成json的问题 很多帖子力聊着聊着就讨论起这个来了. 还有那些依赖包 确实很头疼,我就是找不到那个 不得已用了个旧的代替

  4. #5 楼 · yefeng · 2008-06-13 17:35

    刚用了JSON-lib,有不少地方不符合我的需求,就直接源码拿来,改了一下

  5. #4 楼 · aninfeel · 2008-06-12 14:47

    我做了个applet打印程序,和服务器通过json通信,觉得json-lib牵连太多,而且通过两次转换后对象数据并没有和原来的一致,所以后来干脆用了系列化,效果还不错。

  6. #3 楼 · andrew.yulong · 2008-06-12 10:23

    我如果体格bean里面包含一个set集合,转换json的时候会报错,应该怎么解决》

  7. #2 楼 · shim · 2007-12-20 16:23

    JSON让我觉得有一点不爽,

    Resources resource=manageResourceDao.get(resource.getId());
    JSONArray array = JSONArray.fromObject(resource); //此处出现异常
    
    

    异常如下:

    net.sf.json.JSONException: java.lang.reflect.InvocationTargetException at net.sf.json.JSONObject._fromBean(JSONObject.java:738) at net.sf.json.JSONObject.fromObject(JSONObject.java:182)

    于是我改成

    Resources resource=manageResourceDao.get(resource.getId());
    Resources res=new  Resources();
    BeanUtils.copyProperties(res, resource);
    			
    JSONArray array = JSONArray.fromObject(res);
    

    这样才可以了,觉得这样有些不爽,不知道有没更好的办法解决?

  8. #1 楼 · katelin · 2007-08-09 14:13

    第2点我不会报错

评论功能维护中,当前仅展示历史评论。