在做数据转换为JSON格式时:
import net.sf.json.JSONArray; class T{ private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } public class JSONTest2 { public static void main(String[] args) { T t = new T(); t.setName("zhangsan"); t.setAge(20); JSONArray json = JSONArray.fromObject(t); System.out.println(json); //System.out.println(t.getName()); //System.out.println(t.getAge()); } }
出现了如下错误:
thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: Property 'age' has no getter method in class 'class T'
原因是 声明bean为public class 必须是public