json-lib转化java对象,是否转化为null的属性

时间:2021-02-18 14:58:33
public static void main(String[] args) throws Exception{
User user = new User();
user.setUid(25); JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
@Override
public boolean apply(Object source, String key, Object value) {
return false; // 返回false表示该属性会转化,true表示不转化
}
});
JSONObject json = JSONObject.fromObject(user, jsonConfig );
System.out.println(json.toString());
System.out.println("-----------------------------");
User u1 = (User)JSONObject.toBean(json, User.class); // json转java对象,json中属性不全,一样可以转换成功
System.out.println(u1); }
如果属性为空,且要转化,Integer为null的会转化为0
        <dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
<classifier>jdk15</classifier>
</dependency>