当前位置:   article > 正文

两个JSON对比_对比两个json

对比两个json
  1. 需求:两个Json对比 目前有两个json 类型的数据,{"1":"A","2":"B","3":"C","4":"D"} 和 {"1":"X","2":"Y","3":"Z","5":"Q"}
  2. * 对比出来的结构需要有{"1":{"old":"A","new":"X"},"2":{"old":"B","new":"Y"},"3":{"old":"C","new":"Z"},"4":{"old":"D","new":""},"5":{"old":"","new":"Q"}}
  1. class BO{
  2. /** 行号 **/
  3. private String ROWNO;
  4. /** 名称 **/
  5. private String NAME;
  6. public BO(){
  7. }
  8. public BO(String ROWNO, String NAME) {
  9. this.ROWNO = ROWNO;
  10. this.NAME = NAME;
  11. }
  12. public String getROWNO() {
  13. return ROWNO;
  14. }
  15. public void setROWNO(String ROWNO) {
  16. this.ROWNO = ROWNO;
  17. }
  18. public String getNAME() {
  19. return NAME;
  20. }
  21. public void setNAME(String NAME) {
  22. this.NAME = NAME;
  23. }
  24. }
  25. public class Test {
  26. public void test(){
  27. List<BO> oldList = new ArrayList<>();
  28. BO bo1 = new BO("1","A");
  29. BO bo2 = new BO("2","B");
  30. BO bo3 = new BO("3","C");
  31. BO bo4 = new BO("4","D");
  32. oldList.add(bo1);
  33. oldList.add(bo2);
  34. oldList.add(bo3);
  35. oldList.add(bo4);
  36. List<BO> newList = new ArrayList<>();
  37. BO b1 = new BO("1","X");
  38. BO b2 = new BO("2","Y");
  39. BO b3 = new BO("3","Z");
  40. BO b4 = new BO("5","Q");
  41. newList.add(b1);
  42. newList.add(b2);
  43. newList.add(b3);
  44. newList.add(b4);
  45. List<JSONObject> sonList = new ArrayList();
  46. JSONObject jsonobject = new JSONObject();
  47. Map<String,BO> map = new HashMap<String,BO>();
  48. for(BO b : oldList){
  49. map.put(b.getROWNO(),b);
  50. }
  51. List<BO> intersection = new ArrayList<BO> ();
  52. String ROWNO;
  53. for(BO b: newList) {
  54. ROWNO = b.getROWNO();
  55. if (map.containsKey(ROWNO)){
  56. intersection.add(map.get(ROWNO));
  57. }
  58. }
  59. // 这个时候能确定的是intersection 中的数据可以删掉,剩下的就是比old 多 的
  60. List<BO> copyNewList = new ArrayList<>();
  61. copyNewList.addAll(newList);
  62. List<BO> copyOldList = new ArrayList<>();
  63. copyOldList.addAll(oldList);
  64. // 删除元素
  65. List<BO> removeList = new ArrayList<>();
  66. copyNewList.stream().forEach(vo ->{
  67. for (int j = 0; j < intersection.size(); j++) {
  68. if(vo.getROWNO().equals(intersection.get(j).getROWNO())){
  69. removeList.add(vo);
  70. }
  71. }
  72. });
  73. copyNewList.removeAll(removeList);
  74. for(BO bo:copyNewList){
  75. jsonobject = new JSONObject();
  76. String newName = bo.getNAME();
  77. jsonobject.put("rowNo",bo.getROWNO());
  78. jsonobject.put("oldName","");
  79. jsonobject.put("newName",newName);
  80. sonList.add(jsonobject);
  81. }
  82. // 删除元素
  83. List<BO> tempList = new ArrayList<>();
  84. copyOldList.stream().forEach(vo ->{
  85. for (int j = 0; j < intersection.size(); j++) {
  86. if(vo.getROWNO().equals(intersection.get(j).getROWNO())){
  87. tempList.add(vo);
  88. }
  89. }
  90. });
  91. copyOldList.removeAll(tempList);
  92. System.out.println(copyOldList.size());
  93. for(BO bo:copyOldList){
  94. jsonobject = new JSONObject();
  95. String oldName = bo.getNAME();
  96. jsonobject.put("rowNo",bo.getROWNO());
  97. jsonobject.put("oldName",oldName);
  98. jsonobject.put("newName","");
  99. sonList.add(jsonobject);
  100. }
  101. // 两边相同行号的对比
  102. for (String key : map.keySet()) {
  103. for (int i = 0; i < newList.size(); i++) {
  104. if(key.equals(newList.get(i).getROWNO())){
  105. jsonobject = new JSONObject();
  106. String oldName = map.get(key).getNAME();
  107. String newName = newList.get(i).getNAME();
  108. jsonobject.put("rowNo",key);
  109. jsonobject.put("oldName",oldName);
  110. jsonobject.put("newName",newName);
  111. sonList.add(jsonobject);
  112. }
  113. }
  114. }
  115. // 按照rowNo 排序
  116. Comparator<JSONObject> comparing = comparing((JSONObject a) -> a.getString("rowNo"));
  117. sonList.sort(comparing((JSONObject a) -> a.getString("rowNo")));
  118. System.out.println(sonList);
  119. }
  120. public static void main(String[] args) {
  121. Test t = new Test();
  122. t.test();
  123. }
  124. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/1014752
推荐阅读
相关标签
  

闽ICP备14008679号