`

JSONUtil.bean2Json()报has no read method. SKIPPED问题

阅读更多
分解:
调用方法
JsonUtil.bean2Json(queryHistogramVO,new String[]{}));
将VO对象转换成JSON对象格式
jsonUtil包路径:

queryHistogramVO 对象的属性和方法:

public class HistogramVO {
    private Integer userNum;
    private Integer topCategory;
    private Integer lastUserNum;

    public Integer getCurrentUser() {
        return this.userNum;
    }

    /**
     * @return the topCategory
     */
    public Integer getTopCategory() {
        return topCategory;
    }

    /**
     * @param topCategory the topCategory to set
     */
    public void setTopCategory(Integer topCategory) {
        this.topCategory = topCategory;
    }

    /**
     * @param userNum the userNum to set
     */
    public void setUserNum(Integer userNum) {
        this.userNum = userNum;
    }

    /**
     * @return the lastUserNum
     */
    public Integer getLastUserNum() {
        return lastUserNum;
    }

    /**
     * @param lastUserNum the lastUserNum to set
     */
    public void setLastUserNum(Integer lastUserNum) {
        this.lastUserNum = lastUserNum;
    } 
}肉眼看上去这个类没有任何问题,仔细观察发现 属性"userNum"的get方法为"getCurrentUser()"

详细分析:
1、jsonutil调用类图分析:

 
JsonUtil工具类是通过JSONObject.fromObject()方法转换的,对fromObject详细分析发现代码:

         //这一句话很关键下面详细讲解
         PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors( bean );
         PropertyFilter jsonPropertyFilter = jsonConfig.getJsonPropertyFilter();
         Class beanClass = bean.getClass();
         for( int i = 0; i < pds.length; i++ ){
            String key = pds[i].getName();
            if( exclusions.contains( key ) ){
               continue;
            }

            if( jsonConfig.isIgnoreTransientFields() && isTransientField( key, beanClass ) ){
               continue;
            }

            Class type = pds[i].getPropertyType();
            //判断如果类的get方法存在则设置属性值
            if( pds[i].getReadMethod() != null ){
              //--------------中间的代码省略掉
               setValue( jsonObject, key, value, type, jsonConfig );
            }else{
               //当get方法不存在报警告错误
               String warning = "Property '" + key + "' has no read method. SKIPPED";
               fireWarnEvent( warning, jsonConfig );
               log.warn( warning );
            }
         }PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors( bean );
这段代码是获取Bean所有的属性信息并将他封装成 PropertyDescriptor描述类。
深入 getPropertyDescriptors()分析:

        if (beanClass == null) {
            throw new IllegalArgumentException("No bean class specified");
        }

        // Look up any cached descriptors for this bean class
        PropertyDescriptor[] descriptors = null;
        descriptors =
                (PropertyDescriptor[]) descriptorsCache.get(beanClass);
        if (descriptors != null) {
            return (descriptors);
        }

        // Introspect the bean and cache the generated descriptors
        BeanInfo beanInfo = null;
        try {
            beanInfo = Introspector.getBeanInfo(beanClass);
        } catch (IntrospectionException e) {
            return (new PropertyDescriptor[0]);
        }
        descriptors = beanInfo.getPropertyDescriptors();
        if (descriptors == null) {
            descriptors = new PropertyDescriptor[0];
        }Introspector.getBeanInfo(beanClass);

上面标红的地方是关键部分,他是通过java内省机制获取Bean的属性方法,并返回BeanInfo类。

获取属性的规则:

1、类中包含 公有get方法如: public String getCurrUser()

2、类中包含公有的 set方法如:public void setName(String c)

通过上面的分析,HistogramVO类有setUserNum()方法确没有对应的getUserNum()方法导致报""json.JSONObject - Property 'userNum' has no read method. SKIPPED"警告错误。

添加getUserNum()方法即解决问题。

总结:

这个问题虽然不是很复杂但是把Java的内省机制弄明白了。

相关文档:

http://java.sun.com/products/javabeans/docs/index.html

http://blog.csdn.net/hahalzb/archive/2010/10/28/5972421.aspx



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/liulin_good/archive/2010/12/11/6069797.aspx
分享到:
评论

相关推荐

    JsonUtil json工具类

    JsonUtil json工具类 JsonUtil json工具类

    又小又快又无依赖Json序列化与反序列化

    String json=JsonUtil.instance().obj2Json(obj); 2.json反序列化(Object表示具体的类) Object obj=JsonUtil.instance().json2Obj(json,Object.class) Object[]objs=JsonUtil.instance().json2Obj(json,Object[]...

    JsonUtil.java

    json 工具类 转换 获取泛型的Collection Type JavaType javaType = mapper.getTypeFactory().constructParametricType( collectionClass, elementClasses);

    自己写的jsonUtil.jar

    自己封装的json数据转换类,并打成jar包 可以直接使用

    JsonUtils转化工具

    Json转对象,对象转Json

    JSON 的jar包和js

    JSON是javaWeb开发,使页面与 .

    getbypath:通过路径获取JSON数据

    按路径从JSON对象获取数据。 安装 npm install getbypath 用法 var getByPath = require('getbypath'); var obj = { foo: { bar: { baz: 'thedata', biz: ['one', 'two'] }}}; var bar = getByPath(obj, 'foo.bar....

    Json转对象忽略大小写

    最近在搞一个json的需求,对端提供的json首字母都是大写的。这样转json起来有点难度,从csdn上贴的大神的解决方式,分值比较高。然后自己修改了下,符合自己的需求,就放在这里了。

    json_TO_clientdataset_JSONUtil包.rar

    json_TO_clientdataset_JSONUtil包.rar 整理了一下 JSON 轉CLIENTDATASET 功能 相關單元文件, 從網絡下載的工具類: SUPEROBJECT.PAS, myUnicode.pas , uJSONDB.pas, uDeltaToSQL.pas, superxmlparser.pas 測試...

    json对象与javabean相互转化

    json对象与javabean相互转化 JSONObject jsonObj=JSONObject.fromObject(s);//将字符串转化为json对象 //写数据库处理 ObjBean bean=(ObjBean)JSONObject.toBean(jsonObj,ObjBean.class); ObjBean bean=new ...

    org.json.jar

    org.json的json包文件,只是没有提供javabean直接转化json和json转化javabean,在此基础上添加JSONUtil类,提供了这两个方法,暂时不全希望大家谅解。

    JSONUtil.java

    包含了 通过string转json,通过jsonarray转json,获取json里面的值,包括,boolean,int,long,string,json,jsonarry等类型,同时还可以给json赋值,是一个比较多用途的工具类,同时还进行了异常处理

    JsonUitl(一 个json转成list的简单通用工具类)

    该工具类把json对象数组,转成一个list,通过该list,可以取得对象的属性

    JsonUtil代码工具类

    Json的一个2个工具类,用于Json字符串和Object、List、HashMap的转换。

    引用json所需要jar文件.rar

    在java中引用json时所需要会报各种错误,原因是引入json,同时还要引用json所依赖的各种外部jar包

    JsonUtil工具类

    JsonUtil字符串和对象间转换工具类

    json解析所需jar包

    json解析所需jar包

Global site tag (gtag.js) - Google Analytics