import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import java.util.ArrayList;
import java.util.List;
public class JsonUtils {
/**
* 对象转字符串
*
* @param object
* @return
*/
public static String objectToString(Object object) {
return JSON.toJSONString(object, SerializerFeature.WriteNullStringAsEmpty);
}
/**
* 字符串转对象
*
* @param jsonString
* @return
*/
public static Object stringToObject(String jsonString, Class cla) {
JSONObject jsonObject = JSON.parseObject(jsonString);
Object obj = JSON.toJavaObject(jsonObject, cla);
return obj;
}
/**
* 截取对象中需要的字段
* @param t 需要的截取的对象 可以是List
* @param properties 需要的字段
* @return 返回需要的字段的object对象
*/
public static <T> Object PropertyPreFilter(T t,String... properties){
//如果对象为null
if (t==null)
{
return "";
}
SimplePropertyPreFilter filter = new SimplePropertyPreFilter(t.getClass(),properties);
if(t instanceof List){
if(((List) t).size()==0) {
return new ArrayList<>();
}
SimplePropertyPreFilter listFilter = new SimplePropertyPreFilter(((List)t).get(0).getClass(),properties);
String s = JSONArray.toJSONString(t,listFilter, SerializerFeature.WriteMapNullValue,
// 将String类型的null转成""
SerializerFeature.WriteNullStringAsEmpty,
// 将Number类型的null转成0
SerializerFeature.WriteNullNumberAsZero,
// 将List类型的null转成[]
SerializerFeature.WriteNullListAsEmpty,
// 将Boolean类型的null转成false
SerializerFeature.WriteNullBooleanAsFalse,
// 避免循环引用
SerializerFeature.DisableCircularReferenceDetect);
return JSONArray.parseArray(s);
}
return JSONObject.parseObject(JSON.toJSONString(t,filter,
SerializerFeature.WriteMapNullValue,
// 将String类型的null转成""
SerializerFeature.WriteNullStringAsEmpty,
// 将Number类型的null转成0
SerializerFeature.WriteNullNumberAsZero,
// 将List类型的null转成[]
SerializerFeature.WriteNullListAsEmpty,
// 将Boolean类型的null转成false
SerializerFeature.WriteNullBooleanAsFalse,
// 避免循环引用
SerializerFeature.DisableCircularReferenceDetect));
}
/**
* 从标准Json字符串中获取String值
*
* @param jsonString
* @param key
* @return
*/
public static String getStringValue(String jsonString, String key) {
JSONObject jsonObject = JSON.parseObject(jsonString);
boolean b = jsonObject.containsKey(key);
if (b) {
return jsonObject.getString(key);
} else {
return "";
}
}
/**
* 从Json对象中获取String值
* @param jsonObject
* @param key
* @return
*/
public static String getJsonObjectValue(JSONObject jsonObject, String key) {
boolean b = jsonObject.containsKey(key);
if (b) {
return jsonObject.getString(key);
} else {
return "";
}
}
/**
* json字符串转成List<T>对象
*
* @param jsonString
* @param cla
* @return
*/
public static <T> List<T> stringToList(String jsonString, Class cla) {
List<T> list = new ArrayList<T>();
list = JSON.parseArray(jsonString, cla);
return list;
}
/**
* list集合转json字符串
*
* @param list
* @return
*/
public static <T> String listToJsonString(List<T> list) {
String jsonString = JSONArray.toJSONString(list);
return jsonString;
}
/**
* 从Json字符串中获取int值
*
* @param jsonString
* @param key
* @return
*/
public static int getIntValue(String jsonString, String key) {
JSONObject jsonObject = JSON.parseObject(jsonString);
boolean b = jsonObject.containsKey(key);
if (b) {
return jsonObject.getIntValue(key);
} else {
return 10000;
}
}
public static Boolean existsKey(JSONObject jsonObject, String key) {
return jsonObject.containsKey(key);
}
/**
* 构建单个键值对的Json字符串
*
* @param key
* @param value
* @return
*/
public static String keyValueToString(String key, Object value) {
JSONObject json = new JSONObject();
json.put(key, value);
return json.toJSONString();
}
/**
* 构建含有两个键值对的Json字符串
*
* @param key1
* @param value1
* @param key2
* @param value2
* @return
*/
public static String keyValueToString2(String key1, Object value1, String key2, Object value2) {
JSONObject json = new JSONObject();
json.put(key1, value1);
json.put(key2, value2);
return json.toJSONString();
}
public static String addToJson(String jsonString, String key, Object date) {
JSONObject jsonObject = JSONObject.parseObject(jsonString);
jsonObject.put(key, date);
return jsonObject.toJSONString();
}
}
fastJson 小工具