JSONObject和JSONArray的排序

前端之家收集整理的这篇文章主要介绍了JSONObject和JSONArray的排序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

运行环境:java1.8


方法

  1. /**
  2. * 对jsonarray做比较:size不同,返回false; 排序后的String对比,不相同返回false;
  3. * @param expect
  4. * @param actual
  5. * @return
  6. */
  7. public static boolean compareJsonArray(JSONArray expect,JSONArray actual) {
  8. int expect_size = expect.size();
  9. int actual_size = actual.size();
  10. if (expect_size != actual_size) {
  11. logger.error("the two JSONArrays' size is not equal >>"
  12. + "expect JSONArray's size is [[" + expect.size()
  13. + "]],but actual JSONArray's size is [[" + actual.size()
  14. + "]]");
  15. return false;
  16. }
  17. if (!sortJsonArray(expect).toString().equals(
  18. sortJsonArray(actual).toString())) {
  19. logger.error("the two JSONArrays' value is not equal >>"
  20. + "expect is \r\n" + expect + ",\r\nbut actual is \r\n"
  21. + actual);
  22. return false;
  23. }
  24. return true;
  25. }
  26.  
  27. /**
  28. * JSONObject排序
  29. *
  30. * @param obj
  31. * @return
  32. */
  33. @SuppressWarnings("all")
  34. public static JSONObject sortJsonObject(JSONObject obj) {
  35. Map map = new TreeMap();
  36. Iterator<String> it = obj.keys();
  37. while (it.hasNext()) {
  38. String key = it.next();
  39. Object value = obj.get(key);
  40. if (value instanceof JSONObject) {
  41. // System.out.println(value + " is JSONObject");
  42. map.put(key,sortJsonObject(JSONObject.fromObject(value)));
  43. } else if (value instanceof JSONArray) {
  44. // System.out.println(value + " is JSONArray");
  45. map.put(key,sortJsonArray(JSONArray.fromObject(value)));
  46. } else {
  47. map.put(key,value);
  48. }
  49. }
  50. return JSONObject.fromObject(map);
  51. }
  52.  
  53. /**
  54. * JSONArray排序
  55. *
  56. * @param array
  57. * @return
  58. */
  59. @SuppressWarnings("all")
  60. public static JSONArray sortJsonArray(JSONArray array) {
  61. List list = new ArrayList();
  62. int size = array.size();
  63. for (int i = 0; i < size; i++) {
  64. Object obj = array.get(i);
  65. if (obj instanceof JSONObject) {
  66. list.add(sortJsonObject(JSONObject.fromObject(obj)));
  67. } else if (obj instanceof JSONArray) {
  68. list.add(sortJsonArray(JSONArray.fromObject(obj)));
  69. } else {
  70. list.add(obj);
  71. }
  72. }
  73.  
  74. list.sort((o1,o2) -> o1.toString().compareTo(o2.toString()));
  75. return JSONArray.fromObject(list);
  76. }




测试:

  1. String j1 = "{\"group\":[{\"group_id\":\"10408\",\"group_name\":\"\u4e94\u89d2\u573a\u9ec4\u5174\u5e97\",\"last_uptime\":\"2015-05-20 10:39:16\",\"create_time\":\"2015-05-20 10:30:12\"},{\"group_id\":\"10414\",\"group_name\":\"\u95e8\u5e9798131\",\"last_uptime\":\"2015-05-20 10:40:04\",\"create_time\":\"2015-05-20 10:36:03\"}]}";
  2. String j2 = "{\"group\":[{\"group_id\":\"10408\",\"create_time\":\"2015-05-20 10:36:03\"}]}";
  3. String j3 = "{\"group\":[{\"group_id\":\"10414\",\"create_time\":\"2015-05-20 10:36:03\"},{\"group_id\":\"10408\",\"create_time\":\"2015-05-20 10:30:12\"}]}";
  4. String j4 = "{\"group\":[{\"group_id\":\"10415\",\"create_time\":\"2015-05-20 10:30:12\"}]}";
  5. String j5 = "{\"group\":[{\"group_id\":\"10408\",\"create_time\":\"2015-05-20 10:30:12\"}]}";
  6. String j6 = "{\"group\":[{\"create_time\":\"2015-05-20 10:30:12\",\"group_id\":\"10408\",\"last_uptime\":\"2015-05-20 10:39:16\"},\"create_time\":\"2015-05-20 10:36:03\",\"group_name\":\"\u95e8\u5e9798131\"}]}";
  7. JSONObject o1 = JSONObject.fromObject(j1);
  8. JSONObject o2 = JSONObject.fromObject(j2);
  9. JSONObject o3 = JSONObject.fromObject(j3);
  10. JSONObject o4 = JSONObject.fromObject(j4);
  11. JSONObject o5 = JSONObject.fromObject(j5);
  12. JSONObject o6 = JSONObject.fromObject(j6);
  13. JSONArray a1 = o1.getJSONArray("group");
  14. JSONArray a2 = o2.getJSONArray("group");
  15. JSONArray a3 = o3.getJSONArray("group");
  16. JSONArray a4 = o4.getJSONArray("group");
  17. JSONArray a5 = o5.getJSONArray("group");
  18. JSONArray a6 = o6.getJSONArray("group");
  19. System.out.println("a1 和 a2比较: "
  20. + CompareUtil.compareJsonArray(a1,a2));
  21. System.out.println("a1 和 a3比较: "
  22. + CompareUtil.compareJsonArray(a1,a3));
  23. System.out.println("a1 和 a4比较: "
  24. + CompareUtil.compareJsonArray(a1,a4));
  25. System.out.println("a1 和 a5比较: "
  26. + CompareUtil.compareJsonArray(a1,a5));
  27. System.out.println("a1 和 a6比较: "
  28. + CompareUtil.compareJsonArray(a1,a6));

猜你在找的Json相关文章