1. 源数据
{
"cpCode": "YUNDA",
"unit": null,
"tradeIds": "["20241001"]",
"advantage": [
"读书",
"{"biz":["20241001"],"businessType":"1","siteType":"2","feature":{"identCode":null,"packageId":null,"tradeIds":null,"mfcCreateTime":1711328309000}}",
"讲故事",
"["苹果",42,true,{"name":"张三","age":30,"interests":["阅读","音乐","旅行"]},[1,2,3],null]"
],
"feature": {
"TmsServiceSendReq": "{"biz":["20241001"],"businessType":"1","siteType":"2","feature":{"identCode":null,"packageId":null,"tradeIds":null,"mfcCreateTime":1711328309000}}"
}
}
2. 解析
public class JSONParseUtil {
/**
* @param context 示例,context = "[["20241001","20241002"]]"
*/
public static String parse(String context) {
String jsonType = checkJsonType(context);
if ("JSONObject".equals(jsonType)) {
JSONObject obj = JSON.parseObject(context);
JSONObject resultObj = parseJSONObject(obj);
return JSON.toJSONString(resultObj);
} else if ("JSONArray".equals(jsonType)) {
JSONArray array = JSON.parseArray(context);
JSONArray resultArray = parseJSONArray(array);
return JSON.toJSONString(resultArray);
}
return context;
}
/**
* JSONObject解析(去除转义符)
*/
public static JSONObject parseJSONObject(JSONObject jsonObj) {
JSONObject result = new JSONObject();
for (Map.Entry entry : jsonObj.entrySet()) {
if (entry.getValue() == null) {
result.put(entry.getKey(), entry.getValue());
continue;
}
String value = entry.getValue().toString();
String jsonType = checkJsonType(value);
if ("JSONObject".equals(jsonType)) {
JSONObject parseObject = JSON.parseObject(value);
JSONObject resultObj = parseJSONObject(parseObject);
result.put(entry.getKey(), resultObj);
} else if ("JSONArray".equals(jsonType)) {
JSONArray array = JSON.parseArray(value);
JSONArray resultArray = parseJSONArray(array);
result.put(entry.getKey(), resultArray);
} else {
result.put(entry.getKey(), value);
}
}
return result;
}
/**
* JSONArray解析(去除转义符)
*/
public static JSONArray parseJSONArray(JSONArray jsonArray) {
JSONArray resultArray = new JSONArray();
for (int i = 0; i
3. 解析 – 保留值为null的键
public class JSONParseUtil {
/**
* @param context 示例,context = "[["20241001","20241002"]]"
*/
public static String parse(String context) {
String jsonType = checkJsonType(context);
if ("JSONObject".equals(jsonType)) {
JSONObject obj = JSON.parseObject(context);
JSONObject resultObj = parseJSONObject(obj);
// JSON.toJSONString() 默认情况下序列化会忽略值为null的键
return JSON.toJSONString(resultObj, SerializerFeature.WriteMapNullValue);
} else if ("JSONArray".equals(jsonType)) {
JSONArray array = JSON.parseArray(context);
JSONArray resultArray = parseJSONArray(array);
return JSON.toJSONString(resultArray, SerializerFeature.WriteMapNullValue);
}
return context;
}
/**
* JSONObject解析(去除转义符)
*/
public static JSONObject parseJSONObject(JSONObject jsonObj) {
JSONObject result = new JSONObject();
for (Map.Entry entry : jsonObj.entrySet()) {
Object value = entry.getValue();
if (value == null) {
result.put(entry.getKey(), value);
continue;
}
// 注意:value.toString(),若是 JSONObject 类型,会移除值为null的键
// JSON.toString() 内部实现是 toJSONString(),要先做下类型判断
if (value instanceof JSONObject) {
JSONObject resultObj = parseJSONObject((JSONObject) value);
result.put(entry.getKey(), resultObj);
} else if (value instanceof JSONArray) {
JSONArray resultArray = parseJSONArray((JSONArray) value);
result.put(entry.getKey(), resultArray);
} else {
String data = value.toString();
String jsonType = checkJsonType(data);
if ("JSONObject".equals(jsonType)) {
JSONObject parseObject = JSON.parseObject(data);
JSONObject resultObj = parseJSONObject(parseObject);
result.put(entry.getKey(), resultObj);
} else if ("JSONArray".equals(jsonType)) {
JSONArray array = JSON.parseArray(data);
JSONArray resultArray = parseJSONArray(array);
result.put(entry.getKey(), resultArray);
} else {
result.put(entry.getKey(), value);
}
}
}
return result;
}
/**
* JSONArray解析(去除转义符)
*/
public static JSONArray parseJSONArray(JSONArray jsonArray) {
JSONArray resultArray = new JSONArray();
for (int i = 0; i
4. 正则处理
public static String regexParse(String context) {
// context.replaceAll("\\+", "")
// context.replaceAll("\\+\"", """)
return context.replaceAll("\\+(\")", "$1")
.replaceAll(""\{", "{")
.replaceAll("}"", "}")
.replaceAll(""\[", "[")
.replaceAll("]"", "]");
}
【信息由网络或者个人提供,如有涉及版权请联系COOY资源网邮箱处理】
© 版权声明
部分内容为互联网分享,若有侵权请联系站长删除。
THE END
暂无评论内容