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]; }}
- importjava.util.ArrayList;
- importjava.util.HashMap;
- importjava.util.List;
- importjava.util.Map;
- importnet.sf.json.JSONArray;
- importnet.sf.json.JSONObject;
- publicclassJson{
- publicstaticvoidmain(String[]args){
- Jsonj=newJson();
- j.bean2json();
- }
- publicvoidarr2json(){
- boolean[]boolArray=newboolean[]{true,false,true};
- JSONArrayjsonArray=JSONArray.fromObject(boolArray);
- System.out.println(jsonArray);
- //prints[true,false,true]
- }
- publicvoidlist2json(){
- Listlist=newArrayList();
- list.add("first");
- list.add("second");
- JSONArrayjsonArray=JSONArray.fromObject(list);
- System.out.println(jsonArray);
- //prints["first","second"]
- }
- publicvoidcreateJson(){
- JSONArrayjsonArray=JSONArray.fromObject("['json','is','easy']");
- System.out.println(jsonArray);
- //prints["json","is","easy"]
- }
- publicvoidmap2json(){
- Map
- map.put("name","json");
- map.put("bool",Boolean.TRUE);
- map.put("int",newInteger(1));
- map.put("arr",newString[]{"a","b"});
- map.put("func","function(i){returnthis.arr[i];}");
- JSONObjectjson=JSONObject.fromObject(map);
- System.out.println(json);
- //prints
- //["name":"json","bool":true,"int":1,"arr":["a","b"],"func":function(i){
- //returnthis.arr[i];}]
- }
- publicvoidbean2json(){
- JSONObjectjsonObject=JSONObject.fromObject(newMyBean());
- System.out.println(jsonObject);
- /*
- *prints
- *{"func1":function(i){returnthis.options[i];
- *},"pojoId":1,"name":"json","func2":function(i){return
- *this.options[i];}}
- */
- }
- publicvoidjson2bean(){
- Stringjson="{name=\"json2\",func1:true,pojoId:1,func2:function(a){returna;},options:['1','2']}";
- JSONObjectjb=JSONObject.fromString(json);
- JSONObject.toBean(jb,MyBean.class);
- System.out.println();
- }
- }
- importnet.sf.json.JSONFunction;
- publicclassMyBean{
- privateStringname="json";
- privateintpojoId=1;
- //privatechar[]options=newchar[]{'a','f'};
- privateStringfunc1="function(i){returnthis.options[i];}";
- privateJSONFunctionfunc2=newJSONFunction(newString[]{"i"},
- "returnthis.options[i];");
- //getters&setters
- ......
- }
题外话: 这个我对json-lib包的初次尝试,希望对大家有所帮助,另外大家有谁用过其它处理json的开发包,提出来,大家探讨一下~!!!!