com.fhs.common.utils.JsonUtils Maven / Gradle / Ivy
package com.fhs.common.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
/**
* Json工具类
*
* @author wanglei
* @version [版本号, 2015年9月23日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class JsonUtils {
private static SerializeConfig mapping = new SerializeConfig();
/**
* <把任意对象转换为json字符串 >
*
* @param obj 任意对象
* @return json字符串
*/
public static String object2json(Object obj) {
if (obj == null) {
return "{}";
}
return JSON.toJSONString(obj, mapping, SerializerFeature.WriteMapNullValue);
}
/**
* 去掉value是空的key
*
* @param json json
* @return 处理后的json
*/
public static String clearNullAttr(String json) {
String result = "";
if (!(json.equals(""))) {
LinkedHashMap jsonMap =
JSON.parseObject(json, new TypeReference>() {
});
JSONObject jsonObject = new JSONObject(true);
jsonObject.putAll(jsonMap);
Iterator it = jsonObject.keySet().iterator();
while (it.hasNext()) {
Object keyTemp = it.next();
if (keyTemp == null) {
jsonObject.remove(keyTemp);
}
}
if (jsonObject != null) {
result = JSON.toJSONString(jsonObject);
}
}
return result;
}
/**
* linkedMap2Json 转json
*
* @param map linkedMap linkedMap
* @return json json字符串
*/
public static String linkedMap2Json(LinkedHashMap extends String, ? extends Object> map) {
JSONObject jsonObject = new JSONObject(true);
jsonObject.putAll(map);
return jsonObject.toJSONString();
}
/**
* 先对map的key排序(字典序),然后转换为json返回
*
* @param param 参数map
* @return json
*/
public static String getSortKeyJson(Map param) {
LinkedHashMap result = new LinkedHashMap<>();
List keys = new ArrayList<>(param.keySet());
Collections.sort(keys);
int size = keys.size();
for (int i = 0; i < size; i++) {
result.put(keys.get(i), param.get(keys.get(i)));
}
return JsonUtils.linkedMap2Json(result);
}
public static Map getKeyIndex(Map param) {
LinkedHashMap result = new LinkedHashMap<>();
List keys = new ArrayList<>(param.keySet());
Collections.sort(keys);
int size = keys.size();
for (int i = 0; i < size; i++) {
result.put(keys.get(i), i);
}
return result;
}
/**
* <把bean对象转换为json字符串>
*
* @param bean bean对象
* @return json字符串
*/
public static String bean2json(Object bean) {
return object2json(bean);
}
/**
* <把list对象转换为json字符串>
*
* @param list list对象
* @return json字符串
*/
public static String list2json(List> list) {
if (list == null) {
return "[]";
}
return object2json(list);
}
/**
* <把数组对象转换为json字符串>
*
* @param array 数组对象
* @return json字符串
*/
public static String array2json(Object[] array) {
if (array == null) {
return "[]";
}
return object2json(array);
}
/**
* <把map对象转换为json字符串>
*
* @param map map对象
* @return json字符串
*/
public static String map2json(Map, ?> map) {
return object2json(map);
}
/**
* <把set对象转换为json字符串>
*
* @param set set对象
* @return json字符串
*/
public static String set2json(Set> set) {
if (set == null) {
return "[]";
}
return object2json(set);
}
/**
* <把String对象转换为json字符串>
*
* @param s String对象
* @return json字符串
*/
public static String string2json(String s) {
if (null == s) {
return "";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
switch (ch) {
case '"':
sb.append("\\\\\"");
break;
case '\\':
sb.append("\\\\");
break;
case '\b':
sb.append("\\b");
break;
case '\f':
sb.append("\\f");
break;
case '\n':
sb.append("\\n");
break;
case '\r':
sb.append("\\r");
break;
case '\t':
sb.append("\\t");
break;
case '/':
sb.append("\\/");
break;
default:
if (ch >= '\u0000' && ch <= '\u001F') {
String ss = Integer.toHexString(ch);
sb.append("\\u");
for (int k = 0; k < 4 - ss.length(); k++) {
sb.append('0');
}
sb.append(ss.toUpperCase());
} else {
sb.append(ch);
}
}
}
return sb.toString();
}
/**
* 把json字符串转换为list
*
* @param jsonStr
* @return
*/
public static List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy