com.github.xiaoyuge5201.json.JSONUtils Maven / Gradle / Ivy
package com.github.xiaoyuge5201.json;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
/**
* Created by sead on 17/9/30.
*/
public class JSONUtils {
@SuppressWarnings("unchecked")
public static T parseObject(String json, @SuppressWarnings("rawtypes") Class clazz) {
return (T) JSON.parseObject(json, clazz);
}
@SuppressWarnings("unchecked")
public static T parseArray(String json, @SuppressWarnings("rawtypes") Class clazz) {
return (T) JSON.parseArray(json, clazz);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static T parseArrayNoQ(String json, Class clazz) {
return (T) JSON.parseArray("["+json+"]", clazz);
}
/**
* 输出为空参数
* @param object
* @return
*/
public static String toJSONString(Object object) {
return JSON.toJSONString(object, filter, SerializerFeature.WriteMapNullValue);
}
private static ValueFilter filter = new ValueFilter() {
public Object process(Object obj, String s, Object v) {
if (v == null || "null".equals(v)) {
return "";
}
return v;
}
};
public static Object parse(String json) {
return JSON.parse(json);
}
/**
* 只输出不为空的参数
* @param object
* @return
*/
public static String toJSONStringNonNULL(Object object) {
return JSONObject.toJSONString(object);
}
}