json-lib包笔记
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]; }}
测试类:
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
- public class Json {
- public static void main(String[] args) {
- Json j = new Json();
- j.bean2json();
- }
- public void arr2json() {
- boolean[] boolArray = new boolean[] { true, false, true };
- JSONArray jsonArray = JSONArray.fromObject(boolArray);
- System.out.println(jsonArray);
- // prints [true,false,true]
- }
- public void list2json() {
- List list = new ArrayList();
- list.add("first");
- list.add("second");
- JSONArray jsonArray = JSONArray.fromObject(list);
- System.out.println(jsonArray);
- // prints ["first","second"]
- }
- public void createJson() {
- JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']");
- System.out.println(jsonArray);
- // prints ["json","is","easy"]
- }
- public void map2json() {
- Map
- map.put("name", "json");
- map.put("bool", Boolean.TRUE);
- map.put("int", new Integer(1));
- map.put("arr", new String[] { "a", "b" });
- map.put("func", "function(i){ return this.arr[i]; }");
- JSONObject json = JSONObject.fromObject(map);
- System.out.println(json);
- // prints
- // ["name":"json","bool":true,"int":1,"arr":["a","b"],"func":function(i){
- // return this.arr[i]; }]
- }
- public void bean2json() {
- JSONObject jsonObject = JSONObject.fromObject(new MyBean());
- System.out.println(jsonObject);
- /*
- * prints
- * {"func1":function(i){ return this.options[i];
- * },"pojoId":1,"name":"json","func2":function(i){ return
- * this.options[i]; }}
- */
- }
- public void json2bean() {
- String json = "{name=\"json2\",func1:true,pojoId:1,func2:function(a){ return a; },options:['1','2']}";
- JSONObject jb = JSONObject.fromString(json);
- JSONObject.toBean(jb, MyBean.class);
- System.out.println();
- }
- }
操作的bean:
- import net.sf.json.JSONFunction;
- public class MyBean {
- private String name = "json";
- private int pojoId = 1;
- // private char[] options = new char[] { 'a', 'f' };
- private String func1 = "function(i){ return this.options[i]; }";
- private JSONFunction func2 = new JSONFunction(new String[] { "i" },
- "return this.options[i];");
- // getters & setters
- ......
- }
题外话: 这个我对json-lib包的初次尝试,希望对大家有所帮助,另外大家有谁用过其它处理json的开发包,提出来,大家探讨一下~!!!!
不错不错,今天我也遇到了上面提到的第一个问题,看到你的文章才解决了,多谢多谢。
改写代码吧
一般我反而是遇到返回值是set的就不生成json 因为是一对多关系经常不用加载
什么时候讲讲和hibernate结合自动生成json的问题 很多帖子力聊着聊着就讨论起这个来了. 还有那些依赖包 确实很头疼,我就是找不到那个 不得已用了个旧的代替
刚用了JSON-lib,有不少地方不符合我的需求,就直接源码拿来,改了一下
我做了个applet打印程序,和服务器通过json通信,觉得json-lib牵连太多,而且通过两次转换后对象数据并没有和原来的一致,所以后来干脆用了系列化,效果还不错。
我如果体格bean里面包含一个set集合,转换json的时候会报错,应该怎么解决》
JSON让我觉得有一点不爽,
异常如下:
于是我改成
这样才可以了,觉得这样有些不爽,不知道有没更好的办法解决?
第2点我不会报错