JSON: Property 'xxx' has no getter method in class 'class xxx'
在做数据转换为JSON格式时:
[java] view plain copy
- importnet.sf.json.JSONArray;
- classT{
- privateStringname;
- privateintage;
- publicStringgetName(){
- returnname;
- }
- publicvoidsetName(Stringname){
- this.name=name;
- }
- publicintgetAge(){
- returnage;
- }
- publicvoidsetAge(intage){
- this.age=age;
- }
- }
- publicclassJSONTest2{
- publicstaticvoidmain(String[]args){
- Tt=newT();
- t.setName("zhangsan");
- t.setAge(20);
- JSONArrayjson=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'hasnogettermethodinclass'classT'
原因是 声明bean为public class 必须是public