103 lines
3.1 KiB
Java
103 lines
3.1 KiB
Java
|
package weaver.aiyh_quanshi;
|
||
|
|
||
|
import net.sf.json.JSONArray;
|
||
|
import net.sf.json.JSONObject;
|
||
|
import net.sf.json.JsonConfig;
|
||
|
import net.sf.json.util.JSONUtils;
|
||
|
|
||
|
import java.text.SimpleDateFormat;
|
||
|
import java.util.Date;
|
||
|
import java.util.Map;
|
||
|
|
||
|
public class QsUtil {
|
||
|
static JsonConfig jsonConfig = new JsonConfig();
|
||
|
//设置默认忽略
|
||
|
static {
|
||
|
jsonConfig.setIgnoreDefaultExcludes(false);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @brief 将JSON字符串解析成Map<String, String>类型
|
||
|
* @param[in] json 需要解析的JSON字符串
|
||
|
* @return Map<String, Object> 解析后的Map<String, Object>类型数据
|
||
|
*/
|
||
|
@SuppressWarnings("unchecked")
|
||
|
public static Map<String, Object> jsonSTM(String json) {
|
||
|
return (Map<String, Object>) JSONObject.fromObject(json, jsonConfig);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @brief 转换JSON字符串成JSONObject
|
||
|
* @param[in] json JSON字符串
|
||
|
* @return JSONObject
|
||
|
*/
|
||
|
public static JSONObject jsonSTO(String json){
|
||
|
return JSONObject.fromObject(json, jsonConfig);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @brief 取得JSON字符串中key对应的JSONArray
|
||
|
* @param[in] json JSON字符串
|
||
|
* @param[in] key JSONArray字符串对应的key
|
||
|
* @return key对应的JSONArray
|
||
|
*/
|
||
|
public static JSONArray jsonGetArray(String json, String key) throws Exception{
|
||
|
return jsonSTO(json).getJSONArray(key);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @brief 将Map<String, String>类型数据转换成JSON字符串
|
||
|
* @param[in] map 要封装成JSON字符串的Map<String, String>类型数据
|
||
|
* @return String 组装后的JSON串
|
||
|
*/
|
||
|
public static String jsonMTS(Map<String, Object> map) {
|
||
|
return JSONObject.fromObject(map, jsonConfig).toString();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @brief 将Object类型数据转换成JSON字符串
|
||
|
* @param object 要封装成JSON的Object类型数据对象
|
||
|
* @return String 组装后的JSON串
|
||
|
*/
|
||
|
public static String jsonOTS(Object object) {
|
||
|
if (JSONUtils.isArray(object)) {
|
||
|
return JSONArray.fromObject(object, jsonConfig).toString();
|
||
|
}
|
||
|
return JSONObject.fromObject(object, jsonConfig).toString();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @brief 将Object类型数据组装成Map<String, String>类型数据
|
||
|
* @details
|
||
|
* -# 首先调用jsonOTS转换对象成JSON String类型
|
||
|
* -# 再调用jsonSTM转成JSON String类型为Map<String, String>
|
||
|
*
|
||
|
* @param[in] object 要封装成Map<String, String>类型数据的对象
|
||
|
* @return Map<String, String> 解析后的JSON数据
|
||
|
*/
|
||
|
@SuppressWarnings("unchecked")
|
||
|
public static Map<String, Object> jsonOTM(Object object) {
|
||
|
return (Map<String, Object>)JSONObject.fromObject(object, jsonConfig);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 判断字符串是否为JSON格式
|
||
|
*/
|
||
|
public static boolean mayBeJSON(String jsonStr) {
|
||
|
return JSONUtils.mayBeJSON(jsonStr);
|
||
|
}
|
||
|
|
||
|
public static String getStringFromDate(Date date) {
|
||
|
if (date == null) return null;
|
||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||
|
return sdf.format(date);
|
||
|
}
|
||
|
public static Date addTimes(Date date, Long times) {
|
||
|
if (null == date || null == times)
|
||
|
return null;
|
||
|
|
||
|
long resultTime = date.getTime() + times;
|
||
|
Date resultDate = new Date(resultTime);
|
||
|
return resultDate;
|
||
|
}
|
||
|
}
|