需求是按照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有多少条数据就有多少个warning
。callback
我没有定义这个 属性。
这个不影响项目的运行,但是老是打印控制台和log4j文件,就很头疼了。json
导包是net.sf.json.JSONObject;
jar
是json-lib-2.2.1-jdk15.jar
请问怎么不让它打印。
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()方法即解决问题。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。