public static String readJson(Object obj, List<String> tmpList) {
if(obj instanceof JSONArray) {
JSONArray objArray = (JSONArray)obj;
for (int i = 0; i < objArray.size(); i++) {
if (objArray.get(i) instanceof JSONObject) {
readJson(objArray.get(i), tmpList);
} else if (objArray.get(i) instanceof String) {
tmpList.add(objArray.get(i).toString());
}
}
} else if(obj instanceof JSONObject) {
JSONObject jsonObject = (JSONObject)obj;
Iterator it = jsonObject.keySet().iterator();
while(it.hasNext()) {
String key = it.next().toString();
Object value = jsonObject.get(key);
if(value instanceof JSONArray) {
JSONArray objArray = (JSONArray)value;
readJson(objArray, tmpList);
} else if(value instanceof JSONObject){
readJson((JSONObject)value, tmpList);
} else {
if (StringUtils.isNotBlank(value.toString())) {
String tmpStr = key + "=" + value.toString();
tmpList.add(tmpStr);
}
}
}
} else {
System.out.println("基本类型的参数: {}" + obj.toString());
}
Collections.sort(tmpList);
return tmpList.stream().distinct().collect(Collectors.joining("&"));
}
public static String formatJson(String json_src) {
try {
json_src = StringEscapeUtils.unescapeJavaScript(json_src);
json_src = json_src.replace("\"[", "[");
json_src = json_src.replace("]\"", "]");
json_src = json_src.replace("\"{", "{");
json_src = json_src.replace("}\"", "}");
return json_src;
}catch (Exception e) {
System.out.println("format json error," + e);
return json_src;
}
}
public static String handlerParams(Map<String,String> params) {
JSONObject paramObj = JSON.parseObject(JSON.toJSONString(params));
String postStr = formatJson(paramObj.toJSONString());
List<String> tmpList = new ArrayList<>();
return readJson(JSON.parseObject(postStr), tmpList);
}