JSON: Property 'xxx' has no getter method in class 'class xxx'(转)

原文链接:http://blog.csdn.net/itmyhome1990/article/details/7010528

在做数据转换为JSON格式时:

 

[java] view plain copy
 
  1. import net.sf.json.JSONArray;  
  2.   
  3. class T{  
  4.     private String name;  
  5.     private int age;  
  6.     public String getName() {  
  7.         return name;  
  8.     }  
  9.     public void setName(String name) {  
  10.         this.name = name;  
  11.     }  
  12.     public int getAge() {  
  13.         return age;  
  14.     }  
  15.     public void setAge(int age) {  
  16.         this.age = age;  
  17.     }     
  18. }  
  19. public class JSONTest2 {  
  20.     public static void main(String[] args) {  
  21.         T t = new T();  
  22.         t.setName("zhangsan");  
  23.         t.setAge(20);  
  24.           
  25.         JSONArray json = JSONArray.fromObject(t);  
  26.         System.out.println(json);  
  27.           
  28.         //System.out.println(t.getName());  
  29.         //System.out.println(t.getAge());  
  30.     }  
  31. }  


出现了如下错误:

 

[java] view plain copy
 
  1. thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: Property 'age' has no getter method in class 'class T'  


原因是 声明bean为public  class 必须是public 

posted @ 2017-08-08 13:43  guodaxia  阅读(622)  评论(0)    收藏  举报