mirror of
https://github.com/201206030/novel-plus.git
synced 2025-04-27 01:30:51 +00:00
87 lines
2.0 KiB
Java
87 lines
2.0 KiB
Java
package com.java2nb.common.utils;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public class JSONUtils {
|
|
|
|
/**
|
|
* Bean对象转JSON
|
|
*
|
|
* @param object
|
|
* @param dataFormatString
|
|
* @return
|
|
*/
|
|
public static String beanToJson(Object object, String dataFormatString) {
|
|
if (object != null) {
|
|
if (StringUtils.isEmpty(dataFormatString)) {
|
|
return JSONObject.toJSONString(object);
|
|
}
|
|
return JSON.toJSONStringWithDateFormat(object, dataFormatString);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Bean对象转JSON
|
|
*
|
|
* @param object
|
|
* @return
|
|
*/
|
|
public static String beanToJson(Object object) {
|
|
if (object != null) {
|
|
return JSON.toJSONString(object);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* String转JSON字符串
|
|
*
|
|
* @param key
|
|
* @param value
|
|
* @return
|
|
*/
|
|
public static String stringToJsonByFastjson(String key, String value) {
|
|
if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
|
|
return null;
|
|
}
|
|
Map<String, String> map = new HashMap<String, String>(16);
|
|
map.put(key, value);
|
|
return beanToJson(map, null);
|
|
}
|
|
|
|
/**
|
|
* 将json字符串转换成对象
|
|
*
|
|
* @param json
|
|
* @param clazz
|
|
* @return
|
|
*/
|
|
public static Object jsonToBean(String json, Object clazz) {
|
|
if (StringUtils.isEmpty(json) || clazz == null) {
|
|
return null;
|
|
}
|
|
return JSON.parseObject(json, clazz.getClass());
|
|
}
|
|
|
|
/**
|
|
* json字符串转map
|
|
*
|
|
* @param json
|
|
* @return
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public static Map<String, Object> jsonToMap(String json) {
|
|
if (StringUtils.isEmpty(json)) {
|
|
return null;
|
|
}
|
|
return JSON.parseObject(json, Map.class);
|
|
}
|
|
}
|