阿里巴巴fastjson的jsontools

package com.amarsoft.dataservice.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;

/**
 * 基本操作都忽略key大小写
 */
public class JSONTools {
	
    /**
     * 忽略jsonobject中key大小写取值
     */
	public static void setSimilarValue(JSONObject jsonObj, String key, Object value){
		if (null != jsonObj){
			Set<String> keys = jsonObj.keySet();
			for (String curKey : keys) {
				if (curKey.equalsIgnoreCase(key)) {
					jsonObj.put(curKey, value);
					return;
				}
			}
			jsonObj.put(key, value);
		}
	}
	
	public static void remove(JSONObject jsonObj,String...keys) {
		if (null != jsonObj) {
			Set<String> jsonKeys = jsonObj.keySet();
			Map<String, String> keysMap = new HashMap<>();
			for (String jsonKey : jsonKeys) {
				keysMap.put(jsonKey.toUpperCase(), jsonKey);
			}
			for (String key : keys) {
				if (keysMap.containsKey(key.toUpperCase())) {
					String jsonKey = keysMap.get(key.toUpperCase());
					jsonObj.remove(jsonKey);
				}
			}
		}
	}	

    public static Object getValue(JSONObject jsonObj, String key) {
    	if (jsonObj == null || jsonObj.isEmpty() || StringUtils.isBlank(key)) {
    		return null;
    	}
        Set<String> keys = jsonObj.keySet();
        for (String curKey : keys) {
            if (curKey.equalsIgnoreCase(key)) {
                return jsonObj.get(curKey);
            }
        }

        return null;
    }
    
    public static boolean containsKey(JSONObject jsonObj,String key) {
    	Set<String> keys = jsonObj.keySet();
    	for (String curKey : keys) {
    		if (curKey.equalsIgnoreCase(key)) return true;
    	}
    	    	
    	return false;
    }
    
    public static String getString(JSONObject jsonObj,String key) {
    	Object value = getValue(jsonObj, key);
    	if (value == null) {
    		return null;
    	}
    	
    	return value.toString();
    }
    
    public static String getStringValue(JSONObject jsonObj,String fieldName){
		if (!jsonObj.containsKey(fieldName)) {
			return null;
		}
			
		Object obj = jsonObj.get(fieldName);
		StringBuilder builder = new StringBuilder();
		if (obj != null && obj instanceof JSONObject) {
			JSONObject jsonObjTemp = (JSONObject)obj;
			Set<String> keys = jsonObjTemp.keySet();
			for (String key : keys) {
				Object value = jsonObjTemp.get(key);
				if (value != null) {
					builder.append(key).append("=").append(value).append(";");
				}
			}
			if (builder.length() > 0) {
				builder.deleteCharAt(builder.length() - 1);
			}
		} else {
			builder.append(obj == null ? null : obj.toString());
		}
		
		return builder.toString();
    }
    
    public static String getString(JSONObject jsonObj,String key,String defaultValue) {
    	Object value = getValue(jsonObj, key);
    	if (value == null) {
    		value = defaultValue;
    	}
    	
    	return value.toString();
    }
    
    public static Integer getInt(JSONObject jsonObj,String key) {
    	Object value = getValue(jsonObj, key);
    	if (value == null) {
    		return null;
    	}
    	
    	return parseInteger(key,value);
    }
    
    public static Double getDouble(JSONObject jsonObj,String key) {
    	Object value = getValue(jsonObj, key);
    	if (value == null) {
    		return null;
    	}
    	
    	return parseDouble(value);
    }
    
    public static Double getDouble(JSONObject jsonObj,String key,Double defaultValue) {
    	Object value = getValue(jsonObj, key);
    	if (value == null) {
    		return defaultValue;
    	}
    	
    	return parseDouble(value);
    } 
    
    public static Integer getInt(JSONObject jsonObj,String key,Integer defaultValue) {
    	Object value = getValue(jsonObj, key);
    	if (value == null) {
    		return defaultValue;
    	}
    	
    	return parseInteger(key,value);
    }
    
    public static JSONObject getJSONObject(JSONObject jsonObject,String key) {
    	Object value = getValue(jsonObject, key);
    	if (value == null) {
    		return new JSONObject();
    	}
    	
    	if (!(value instanceof JSONObject)) {
    		throw new RuntimeException("Key:"+key+"属性值不是JSON对象,原始值为:"+value);
    	}
    	return (JSONObject) value;
    }
    
    public static JSONArray getJSONArray(JSONObject jsonObject,String key) {
    	Object value = getValue(jsonObject, key);
    	if (value == null) {
    		return new JSONArray();
    	}
    	
    	if (!(value instanceof JSONArray)) {
    		throw new RuntimeException("Key:"+key+"属性值不是JSONArray,原始值为:"+value);
    	}
    	
    	return (JSONArray) value;
    }
    
    private static Integer parseInteger(String key,Object value) {
    	if (value instanceof String) {
    		String regex = "\\d+";
    		String s = (String) value;
    		if (s.matches(regex)) {
    			return Integer.parseInt(s);
    		} else if ("null".equalsIgnoreCase(s)) {
    			return null;
    		}
    		throw new RuntimeException("无法将value:"+value+"转换成int型");
    	}
    	if (!(value.getClass() == int.class || value instanceof Integer)) {
    		throw new RuntimeException("无法将value:"+value+"转换成int型");
    	}
    	
    	return (int)value;
    }
    
    
    private static Double parseDouble(Object value) {
    	if (value instanceof String) {
    		String s = (String) value;
    		String regex = "\\d+(\\.\\d+)?";
    		if (s.matches(regex)) {
    			return Double.parseDouble(s);
    		} else if ("null".equalsIgnoreCase(s)) {
    			return null;
    		}
    		throw new RuntimeException("无法将value:"+value+"转换成Double型");
    	} else {
    		if (!(value.getClass() == double.class || value instanceof Double)) {
        		throw new RuntimeException("无法将value:"+value+"转换成Double型");
        	}
        	
        	return (Double) value; 
    	}
    }
    
   
    
    public static JSONArray replaceValue2JSONArray(JSONArray array,String inputParam,String paramValue) {
    	JSONArray jsonArray = new JSONArray();
    	if (array == null || array.isEmpty()) {
    		return jsonArray;
    	}
    	
    	for (int i = 0; i < array.size(); i++) {
    		JSONObject jsonObject2 = array.getJSONObject(i);

    		Set<String> keys = jsonObject2.keySet();
    	    for (String curKey : keys) {
    	        if (curKey.equalsIgnoreCase(inputParam)) {
    	        	jsonObject2.put(curKey, paramValue) ;
    	        }
    	    }
    	    
    	    jsonArray.add(jsonObject2);
    	
		}
    	
    	return jsonArray;
    }
    
    
    public static String toJSONString(JSONObject jsonObj) {
    	return JSONObject.toJSONString(jsonObj
    			,VALUE_FILTER
    			,SerializerFeature.WriteNullListAsEmpty
    			,SerializerFeature.WriteNullStringAsEmpty
    			);
    }
    
    
    private static final ValueFilter VALUE_FILTER = new ValueFilter() {
		@Override
		public Object process(Object object, String name, Object value) {
			if (value == null) {
				return "";
			}
			
			return value;
		}
	};
    
}

猜你喜欢

转载自blog.csdn.net/John_Kry/article/details/90901731