合并JsonObj
public static void mergeJSONTo(JSONObject main, JSONObject b) { JSONObject rtn = main; for (String key : b.keySet()) { if (rtn.containsKey(key)) { Object o = rtn.get(key); Object bO = b.get(key); if (o instanceof List && bO instanceof List) { List rA = (List) o; List bA = (List) bO; rA.addAll(bA); } else if (o instanceof Number && bO instanceof Number) { if (o instanceof Double) { Double na = (Double) o; Double nb = (Double) bO; rtn.put(key, na + nb); } else if (o instanceof Float) { Float na = (Float) o; Float nb = (Float) bO; rtn.put(key, na + nb); } else if (o instanceof Long) { Long na = (Long) o; Long nb = (Long) bO; rtn.put(key, na + nb); } else if (o instanceof Integer) { Integer na = (Integer) o; Integer nb = (Integer) bO; rtn.put(key, na + nb); } else if (o instanceof Short) { Short na = (Short) o; Short nb = (Short) bO; rtn.put(key, na + nb); } else if (o instanceof Byte) { Byte na = (Byte) o; Byte nb = (Byte) bO; rtn.put(key, na + nb); } else { System.err.println("[Add] Can't merge JSONObJ for Error Type for A : " + o.getClass().toString() + " B: " + bO.getClass().toString()); } } else if (o instanceof JSONObject && bO instanceof JSONObject) { rtn.put(key, mergeJSON(rtn.getJSONObject(key), b.getJSONObject(key))); //保留 A的 } else { System.err.println("[Arrays] Can't merge JSONObJ for Error Type for A : " + o.getClass().toString() + " B: " + bO.getClass().toString()); } } else { rtn.put(key, b.get(key)); } } } /** * 合并json 出现数组自动合并 */ public static JSONObject mergeJSON(JSONObject a, JSONObject b) { JSONObject rtn = fromObject(a); for (String key : b.keySet()) { if (rtn.containsKey(key)) { Object o = rtn.get(key); Object bO = b.get(key); if (o instanceof List && bO instanceof List) { List rA = (List) o; List bA = (List) bO; rA.addAll(bA); } else if (o instanceof Number && bO instanceof Number) { if (o instanceof Double) { Double na = (Double) o; Double nb = (Double) bO; rtn.put(key, na + nb); } else if (o instanceof Float) { Float na = (Float) o; Float nb = (Float) bO; rtn.put(key, na + nb); } else if (o instanceof Long) { Long na = (Long) o; Long nb = (Long) bO; rtn.put(key, na + nb); } else if (o instanceof Integer) { Integer na = (Integer) o; Integer nb = (Integer) bO; rtn.put(key, na + nb); } else if (o instanceof Short) { Short na = (Short) o; Short nb = (Short) bO; rtn.put(key, na + nb); } else if (o instanceof Byte) { Byte na = (Byte) o; Byte nb = (Byte) bO; rtn.put(key, na + nb); } else { System.err.println("[Add] Can't merge JSONObJ for Error Type for A : " + o.getClass().toString() + " B: " + bO.getClass().toString()); } } else if (o instanceof JSONObject && bO instanceof JSONObject) { rtn.put(key, mergeJSON(rtn.getJSONObject(key), b.getJSONObject(key))); //保留 A的 } else { System.err.println("[Arrays] Can't merge JSONObJ for Error Type for A : " + o.getClass().toString() + " B: " + bO.getClass().toString()); } } else { rtn.put(key, b.get(key)); } } return rtn; } //数组合并测试 public static void main(String[] args) { /*JSONObject o = new JSONObject(); JSONArray a = new JSONArray(); JSONArray aa = new JSONArray(); for (int i = 0; i < 5; i++) { a.add("aaaaa" + i); aa.add("a1a1a1" + i); } o.put("aa", a); o.put("a1", aa); JSONObject t = new JSONObject(); JSONArray bb = new JSONArray(); JSONArray b = new JSONArray(); for (int i = 0; i < 5; i++) { b.add("bbbbb" + i); bb.add("b1b1b1" + i); } t.put("aa", b); t.put("a1", bb);*/ JSONObject a = new JSONObject(); JSONObject b = new JSONObject(); JSONObject aa = new JSONObject(); JSONObject bb = new JSONObject(); a.put("t", aa); b.put("t", bb); for (int i = 1; i < 4; i++) { aa.put("a" + i, 1); bb.put("a" + i, i); } System.out.println(a.toJSONString()); System.out.println(b.toJSONString()); System.out.println("merge->" + mergeJSON(a, b).toJSONString()); System.out.println(a.toJSONString()); System.out.println(b.toJSONString()); mergeJSONTo(a, b); System.out.println("mergeTo->" + a.toJSONString()); System.out.println(a.toJSONString()); System.out.println(b.toJSONString()); }
本文作者:StanWind
文章标题: 合并JsonObj
本文地址:https://www.stanwind.com/post/68
版权声明:若无注明,本文皆为“Make it Better”原创,转载请保留文章出处。
本文地址:https://www.stanwind.com/post/68
版权声明:若无注明,本文皆为“Make it Better”原创,转载请保留文章出处。
相关文章