调用第三方api接口,别人返回的是JSONString,所以我们自己拿到还要重新转义成JSON,但是jsonString可能中属性会有嵌套json数组或者json对象。所以写了一个工具类来转化。
效果图:
原JsonString
1.
2.
转义后
1.
2.
引用fastjson包来做转义
com.alibaba fastjson1.2.41
转换工具类:JsonUtils
package com.szmsd.hulutianxia.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
import java.util.Set;
@Slf4j
public class JsonUtils {
public static Object jsonStrToJSonRecursion(String json){
if (json==null) {
return null;
}
try {
JSONObject jsonObject = JSONObject.parseObject(json);
Object object = null;
Set> entries = jsonObject.entrySet();
for (Map.Entry entry: entries) {
String key = entry.getKey();
Object value = entry.getValue();
if (value == null) {
continue;
}
if (jsonObject.getString(key).startsWith("[")) {
//属性值是json数组
JSONArray jsonArray = JSONArray.parseArray(entry.getValue().toString());
JSONArray copyArray = new JSONArray();
for (int i =0 ,len = jsonArray.size();i



