net.sf.json.JSONObject null对象自动转换为0的解决方式

    技术2022-07-17  62

    在开发中将对象转换为json对象常用的有net.sf.json.JSONObject,但是经常遇到某个对象为null时前端转换结果为0。当某个字段0本身就具有含义时,会导致前端无法判断该字段为0还是为null。

     例如

    UserVo user = new UserVo(); user.setPhone("12345"); user.setDept(new DeptVo()); json.put("user", user); System.err.println(json);

    输出结果

    {"user":{"dept":{"airport":"","deptNature":0,"deptshort":"","describ":"","dutycall":"","dutyfax":"","id":0,"leve":"","name":"","parentid":0,"ranking":0,"signing":0,"signing2":0,"signing3":0},"deptId":0,"email":"","fleet":"","personType":"","phone":"12345","sex":0,"staffName":"","staffNum":""}}

    解决方式

    设置自定义的转换方式

    net.sf.json.JSONObject中支持设置JsonConfig来实现对null对象的操作

    UserVo user = new UserVo(); user.setPhone("12345"); user.setDept(new DeptVo()); // 自定义Integer为null时的返回值,这里设置为空串 JsonConfig jsonConfig0 = new JsonConfig(); jsonConfig0.registerDefaultValueProcessor(Integer.class, new DefaultDefaultValueProcessor() { public Object getDefaultValue(Class type) { return ""; } }); json.put("user", JSONObject.fromObject(user, jsonConfig0)); System.err.println(json); {"user":{"dept":{"airport":"","deptNature":"","deptshort":"","describ":"","dutycall":"","dutyfax":"","id":"","leve":"","name":"","parentid":"","ranking":"","signing":"","signing2":"","signing3":""},"deptId":"","email":"","fleet":"","personType":"","phone":"12345","sex":"","staffName":"","staffNum":""}}

    如果需要对Double,Float等进行设置,则需要设置多次

    import java.util.HashMap; import java.util.Map; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.processors.DefaultDefaultValueProcessor; /** * 自定义JsonConfig 用于配置属性为null时的转换结果 * @author * */ public class SpeJsonConfig extends JsonConfig { /** * spe 自定义转成结果 * * @param settingMap * @return */ public static JsonConfig getSpeJsonConfig(Map<Class, String> settingMap) { JsonConfig jsonConfig = new JsonConfig(); for (Class temp : settingMap.keySet()) { jsonConfig.registerDefaultValueProcessor(temp, new DefaultDefaultValueProcessor() { public Object getDefaultValue(Class type) { return settingMap.get(temp); } }); } return jsonConfig; } /** * default 默认将null转成 空字符串 "" * @return */ public static JsonConfig getNull2EmptyJsonConfig() { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerDefaultValueProcessor(Double.class, new DefaultDefaultValueProcessor() { public Object getDefaultValue(Class type) { return ""; } }); jsonConfig.registerDefaultValueProcessor(Float.class, new DefaultDefaultValueProcessor() { public Object getDefaultValue(Class type) { return ""; } }); jsonConfig.registerDefaultValueProcessor(Integer.class, new DefaultDefaultValueProcessor() { public Object getDefaultValue(Class type) { return ""; } }); jsonConfig.registerDefaultValueProcessor(Boolean.class, new DefaultDefaultValueProcessor() { public Object getDefaultValue(Class type) { return ""; } }); return jsonConfig; } public static void main(String[] args) { JSONObject json = new JSONObject(); // 自定义转换设置 Map<Class, String> setting = new HashMap<Class, String>(); setting.put(Integer.class, "Integer is null"); setting.put(Double.class, "Double is null"); setting.put(Float.class, "Float is null"); setting.put(Boolean.class, "Boolean is null"); JsonConfig config = SpeJsonConfig.getSpeJsonConfig(setting); // 默认转空设置 JsonConfig emptyConfig = SpeJsonConfig.getNull2EmptyJsonConfig(); // 值 Map<String, Object> valueMap = new HashMap<String, Object>(); UserVo user = new UserVo(); user.setPhone("12345"); user.setDept(new DeptVo()); JsonConfig jsonConfig0 = new JsonConfig(); jsonConfig0.registerDefaultValueProcessor(Integer.class, new DefaultDefaultValueProcessor() { public Object getDefaultValue(Class type) { return ""; } }); json.put("user", JSONObject.fromObject(user, jsonConfig0)); System.err.println(json); valueMap.put("user", user); JsonConfig jsonConfig = SpeJsonConfig.getSpeJsonConfig(setting); json = JSONObject.fromObject(user, jsonConfig); json.put("user", JSONObject.fromObject(user, jsonConfig)); json.putAll(valueMap, emptyConfig); System.err.println(json); } }

     

    Processed: 0.016, SQL: 9