net.sf.json.JSONObject null對象自動轉換爲0的解決方式

在開發中將對象轉換爲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);
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章