您的位置:首页 > 移动开发 > Objective-C

修改JSONObject.fromObject数字为null时被转换为0

2020-03-01 11:08 736 查看
如果未设置的话默认是DefaultDefaultValueProcessor


public class DefaultDefaultValueProcessor implements DefaultValueProcessor {
public Object getDefaultValue( Class type ) {
if( JSONUtils.isArray( type ) ){
return new JSONArray();
}else if( JSONUtils.isNumber( type ) ){
if( JSONUtils.isDouble( type ) ){
return new Double( 0 );
}else{
return new Integer( 0 );
}
}else if( JSONUtils.isBoolean( type ) ){
return Boolean.FALSE;
}else if( JSONUtils.isString( type ) ){
return "";
}
return JSONNull.getInstance();
}
}



在jsonConfig 注册defaultValueProcessor


// 设置Integer类型为空的默认值 json-lib默认是0
jsonConfig.registerDefaultValueProcessor(Integer.class,
new DefaultValueProcessor() {
public Object getDefaultValue(Class type) {
return null;
}
});



这样转换时Integer类型如果为null转换还是null,不会被转为0
  • 点赞
  • 收藏
  • 分享
  • 文章举报
iteye_5075 发布了3 篇原创文章 · 获赞 0 · 访问量 499 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: