12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package thyyxxk.webserver.utils;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import java.util.Map;
- /**
- * json工具类
- *
- * @Version 1.0
- */
- public final class JsonUtils {
- /**
- * 去除json值前后空格
- *
- * @param jsonStr jsonStr
- * @return
- */
- public static JSONObject jsonTrim(String jsonStr) {
- return jsonTrim(JSONObject.parseObject(jsonStr));
- }
- public static String jsonTrimToString(String jsonStr) {
- return jsonTrim(JSONObject.parseObject(jsonStr)).toJSONString();
- }
- /**
- * 去除value的空格
- *
- * @param jsonObject jsonObject
- */
- public static JSONObject jsonTrim(JSONObject jsonObject) {
- for (Map.Entry<String, Object> next : jsonObject.entrySet()) {
- Object value = next.getValue();
- if (value != null) {
- if (value instanceof String) {
- //清空值前后空格
- jsonObject.put(next.getKey(), ((String) value).trim());
- } else if (value instanceof JSONObject) {
- jsonTrim((JSONObject) value);
- } else if (value instanceof JSONArray) {
- jsonTrimArray((JSONArray) value);
- }
- }
- }
- return jsonObject;
- }
- /**
- * 清空JSONArray 值前后空格
- *
- */
- private static void jsonTrimArray(JSONArray array) {
- if (array != null && !array.isEmpty()) {
- for (int i = 0; i < array.size(); i++) {
- Object object = array.get(i);
- if (object != null) {
- if (object instanceof String) {
- array.set(i, ((String) object).trim());
- } else if (object instanceof JSONObject) {
- jsonTrim((JSONObject) object);
- } else if (object instanceof JSONArray) {
- jsonTrimArray((JSONArray) object);
- }
- }
- }
- }
- }
- }
|