开发者社区> 问答> 正文

list转json出现警告的问题

需求是按照easyui来转json。代码是这样的
`JSONObject jsonObj = new JSONObject();
jsonObj.put("total",total );
jsonObj.put("rows", list);`
total是总页数,list是数据集合。
但是控制台一直有warning
net.sf.json.JSONObject - Property 'callback' has no read method. SKIPPED
list有多少条数据就有多少个warningcallback我没有定义这个 属性。
这个不影响项目的运行,但是老是打印控制台和log4j文件,就很头疼了。
json导包是net.sf.json.JSONObject;
jarjson-lib-2.2.1-jdk15.jar
请问怎么不让它打印。

展开
收起
蛮大人123 2016-03-17 17:22:05 4740 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    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()方法即解决问题。

    2019-07-17 19:05:40
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载