ssjygl-xsct-service/ssjygl-xsx-common/src/main/java/com/cowr/common/utils/JsonUtil.java

41 lines
1.1 KiB
Java
Raw Normal View History

2020-08-07 17:11:12 +08:00
package com.cowr.common.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class JsonUtil {
2020-10-18 00:39:17 +08:00
public static JSONObject key2Lower(JSONObject obj) {
2020-08-07 17:11:12 +08:00
return key2Lower(obj, false);
}
/**
* json key
2020-10-18 00:39:17 +08:00
*
2020-08-07 17:11:12 +08:00
* @param obj
* @param deep
* @return
*/
2020-10-18 00:39:17 +08:00
public static JSONObject key2Lower(JSONObject obj, boolean deep) {
if (obj == null || obj.isEmpty()) {
2020-08-07 17:11:12 +08:00
return obj;
}
JSONObject out = new JSONObject();
2020-10-18 00:39:17 +08:00
for (String key : obj.keySet()) {
2020-08-07 17:11:12 +08:00
Object tmp = obj.get(key);
2020-10-18 00:39:17 +08:00
if (tmp instanceof JSONObject && deep) {
tmp = key2Lower((JSONObject) tmp, deep);
} else if (tmp instanceof JSONArray && deep) {
2020-08-07 17:11:12 +08:00
JSONArray arr = (JSONArray) tmp;
2020-10-18 00:39:17 +08:00
for (int i = 0; i < arr.size(); i++) {
2020-08-07 17:11:12 +08:00
arr.set(i, key2Lower(arr.getJSONObject(i), deep));
}
}
out.put(key.toLowerCase(), tmp);
}
return out;
}
}