小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

java jason包的選擇, jason-lib 錯(cuò)誤 "has no getter m...

 歡歡2008 2011-06-23


 

看到網(wǎng)上有人用jason-lib這個(gè)庫(kù),我就選擇了它!開(kāi)始學(xué)習(xí)的時(shí)候碰到了一些問(wèn)題,但只要按照要求配置好下面四個(gè)依賴庫(kù)就可以寫測(cè)試代碼了。
jakarta commons-lang 2.3
jakarta commons-beanutils 1.7.0
jakarta commons-collections 3.2
jakarta commons-logging 1.1.1
ezmorph 1.0.4
然后按照網(wǎng)站上的getting started進(jìn)行學(xué)習(xí),碰到的第一個(gè)問(wèn)題就是在處理bean的轉(zhuǎn)換的時(shí)候總是報(bào)告"has no getter method",找了好久才知道,
錯(cuò)誤的原因是要轉(zhuǎn)換的bean必須是public的。
下面是我找到的學(xué)習(xí)資料:

相關(guān)鏈接:
http://json-lib./
http://ezmorph./
http://morph./

使用過(guò)程中問(wèn)題:
1,把bean轉(zhuǎn)化為json格式時(shí)老提示如下錯(cuò)誤:
Exception in thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: Property 'name' has no getter method
解決:聲明bean為public class xxx,必須是public,我用默認(rèn)類型(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];");
   轉(zhuǎn)換后顯示結(jié)果差不多:
   {"func1":function(i){ return this.options[i];,"func2":function(i){ return this.options[i]; }}

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


 

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

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多