Skip to content

Instantly share code, notes, and snippets.

@KomiSans

KomiSans/jsonDiff Secret

Created May 2, 2021
Embed
What would you like to do?
JSONObject 内容比对
// StringUtil类是我们公司自己提供的
public static String diffJson(JSONObject obj, String pref, StringBuilder strb, boolean isArray) {
String strb_retri = "";
if (StringUtil.isEmpty(pref)) {
strb.append("{");
}
for (String keyIn : obj.keySet()) {
if (obj.get(keyIn) instanceof JSONObject) {
strb.append(diffJson((JSONObject) obj.get(keyIn), pref + keyIn + ".", new StringBuilder().append(strb_retri), isArray));
} else if (obj.get(keyIn) instanceof JSONArray) {
JSONArray ar = (JSONArray) obj.get(keyIn);
strb.append("'" + pref + keyIn + "':[");
for (int idx = 0; idx < ar.size(); idx++) {
if (ar.get(idx) instanceof JSONObject) {
strb.append(diffJson((JSONObject) ar.get(idx), pref + keyIn + ".", new StringBuilder().append(strb_retri), true));
} else {
strb.append(ar.get(idx).toString() + "");
}
}
strb.append("] xto ");
} else {
strb.append("'" + pref + keyIn + "':'" + obj.get(keyIn) + "' xto ");
}
}
if (StringUtil.isEmpty(pref)) {
strb_retri = strb.toString();
int lastOfSymbol = strb_retri.lastIndexOf(" xto ");
strb_retri = strb_retri.substring(0, lastOfSymbol) + strb_retri.substring(lastOfSymbol + 1);
strb_retri += "}";
} else {
strb_retri = strb.toString();
}
return strb_retri;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment