将JSON对象中的某个字段进行分组和排序(java实现)

将JSON对象中的某个字段进行分组和排序(java实现)

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说将JSON对象中的某个字段进行分组和排序(java实现),希望能够帮助大家进步!!!

现在有这样一个数据

 
  1. {

  2. "systemid": "123",

  3. "productid": "123",

  4. "dataArray": [

  5. {

  6. "line_num": 2,

  7. "subjectcodecv": 1,

  8. "subjectname": "公司",

  9. "subjectid": "1001",

  10. "resource_members_id": "003",

  11. "resource_members_name": "xx"

  12. }, {

  13. "line_num": 2,

  14. "subjectcodecv": 2,

  15. "subjectname": "产品",

  16. "subjectid": "10",

  17. "resource_members_id": "002",

  18. "resource_members_name": "11xx"

  19. }, {

  20. "line_num": 2,

  21. "subjectcodecv": 3,

  22. "subjectname": "产品",

  23. "subjectid": "10",

  24. "resource_members_id": "001",

  25. "resource_members_name": "22xx"

  26. }, {

  27. "line_num": 1,

  28. "subjectcodecv": 1,

  29. "subjectname": "公司",

  30. "subjectid": "1001",

  31. "resource_members_id": "005",

  32. "resource_members_name": "xx"

  33. }, {

  34. "line_num": 1,

  35. "subjectcodecv": 2,

  36. "subjectname": "产品",

  37. "subjectid": "10",

  38. "resource_members_id": "004",

  39. "resource_members_name": "11xx"

  40. }, {

  41. "line_num": 1,

  42. "subjectcodecv": 3,

  43. "subjectname": "产品",

  44. "subjectid": "10",

  45. "resource_members_id": "003",

  46. "resource_members_name": "22xx"

  47. }

  48. ]

  49. }

需求:将JSON字符串中的dataArray数组中的json对象按line_num字段进行分组,然后将每组中的json对象按照resource_members_id字段进行排序(后续还有对每组排序后的json对象进行业务操作,这里不做)

实现:

 
  1. package com.feidao.test;

  2.  
  3. import com.alibaba.fastjson.JSON;

  4. import com.alibaba.fastjson.JSONArray;

  5. import com.alibaba.fastjson.JSONObject;

  6.  
  7. import java.util.ArrayList;

  8. import java.util.List;

  9. import java.util.Map;

  10. import java.util.stream.Collectors;

  11.  
  12. /**

  13. * author: LN create: 2019-05-23 09:40

  14. */

  15. public class GroupByTest {

  16. public static void main(String[] args) {

  17. String jsonStr = "{\n" +

  18. "\t\"systemid\": \"123\",\n" +

  19. "\t\"productid\": \"123\",\n" +

  20. "\t\"dataArray\": [\n" +

  21. "\t\t{\n" +

  22. "\t\t\t\"line_num\": 2,\n" +

  23. "\t\t\t\"subjectcodecv\": 5,\n" +

  24. "\t\t\t\"subjectname\": \"公司\",\n" +

  25. "\t\t\t\"subjectid\": \"1001\",\n" +

  26. "\t\t\t\"resource_members_id\": \"003\",\n" +

  27. "\t\t\t\"resource_members_name\": \"xx\"\n" +

  28. "\t\t}, {\n" +

  29. "\t\t\t\"line_num\": 2,\n" +

  30. "\t\t\t\"subjectcodecv\": 4,\n" +

  31. "\t\t\t\"subjectname\": \"产品\",\n" +

  32. "\t\t\t\"subjectid\": \"10\",\n" +

  33. "\t\t\t\"resource_members_id\": \"002\",\n" +

  34. "\t\t\t\"resource_members_name\": \"11xx\"\n" +

  35. "\t\t}, {\n" +

  36. "\t\t\t\"line_num\": 2,\n" +

  37. "\t\t\t\"subjectcodecv\": 3,\n" +

  38. "\t\t\t\"subjectname\": \"产品\",\n" +

  39. "\t\t\t\"subjectid\": \"10\",\n" +

  40. "\t\t\t\"resource_members_id\": \"001\",\n" +

  41. "\t\t\t\"resource_members_name\": \"22xx\"\n" +

  42. "\t\t}, {\n" +

  43. "\t\t\t\"line_num\": 1,\n" +

  44. "\t\t\t\"subjectcodecv\": 3,\n" +

  45. "\t\t\t\"subjectname\": \"公司\",\n" +

  46. "\t\t\t\"subjectid\": \"1001\",\n" +

  47. "\t\t\t\"resource_members_id\": \"005\",\n" +

  48. "\t\t\t\"resource_members_name\": \"xx\"\n" +

  49. "\t\t}, {\n" +

  50. "\t\t\t\"line_num\":1,\n" +

  51. "\t\t\t\"subjectcodecv\": 1,\n" +

  52. "\t\t\t\"subjectname\": \"产品\",\n" +

  53. "\t\t\t\"subjectid\": \"10\",\n" +

  54. "\t\t\t\"resource_members_id\": \"004\",\n" +

  55. "\t\t\t\"resource_members_name\": \"11xx\"\n" +

  56. "\t\t}, {\n" +

  57. "\t\t\t\"line_num\": 1,\n" +

  58. "\t\t\t\"subjectcodecv\": 5,\n" +

  59. "\t\t\t\"subjectname\": \"产品\",\n" +

  60. "\t\t\t\"subjectid\": \"10\",\n" +

  61. "\t\t\t\"resource_members_id\": \"003\",\n" +

  62. "\t\t\t\"resource_members_name\": \"22xx\"\n" +

  63. "\t\t}\n" +

  64. "\t]\n" +

  65. "}";

  66.  
  67. JSONObject originalJsonData = JSONObject.parseObject(jsonStr);

  68. JSONArray dataArray = originalJsonData.getJSONArray("dataArray");

  69.  
  70. List<JSONObject> jsonObjectList = new ArrayList<>();

  71. //将原始数据中的json数组中的json对象存到一个list集合中

  72. for (Object o : dataArray) {

  73. jsonObjectList.add((JSONObject) o);

  74. }

  75.  
  76. //对集合中的json对象进行分组

  77. //然后返回一个map集合,key代表组名,value代表该组中的数据

  78. Map<String, List<JSONObject>> groupByLineNumData = jsonObjectList.stream()

  79. .collect(Collectors.groupingBy(x -> x.getString("line_num")));

  80.  
  81. //遍历所有的分组,对每组内的JSON对象进行排序

  82. for (String s : groupByLineNumData.keySet()) {

  83. List<JSONObject> list = groupByLineNumData.get(s);

  84. JSONArray jsonSorted = jsonArraySort(list.toString());

  85. //后续有对排序后的json对象的业务操作

  86. //这里只将它输出

  87. System.out.println(jsonSorted);

  88. }

  89.  
  90. }

  91.  
  92. /**

  93. * 对多个JOSN对象进行自定义字段排序

  94. *

  95. * @param jsonArrStr 要排序的JSON数据

  96. * @return 返回排序后的结果

  97. */

  98. private static JSONArray jsonArraySort(String jsonArrStr) {

  99. JSONArray jsonArr = JSON.parseArray(jsonArrStr);

  100. JSONArray sortedJsonArray = new JSONArray();

  101. List<JSONObject> jsonValues = new ArrayList<JSONObject>();

  102. //存放json数组中的每个json对象

  103. for (int i = 0; i < jsonArr.size(); i++) {

  104. jsonValues.add(jsonArr.getJSONObject(i));

  105. }

  106.  
  107. //对集合中的JOSN对象进行自定义排序

  108. jsonValues.sort((a, b) -> {

  109. String valA = a.getString("resource_members_id");

  110. String valB = b.getString("resource_members_id");

  111. return valA.compareTo(valB);

  112. });

  113.  
  114. for (int i = 0; i < jsonArr.size(); i++) {

  115. sortedJsonArray.add(jsonValues.get(i));

  116. }

  117. return sortedJsonArray;

  118. }

  119. }

输出结果:

输出了两个排序好的JSON数组

line_num为1:

resource_members_id也排好序了

 
  1. [

  2. {

  3. "subjectname": "产品",

  4. "resource_members_name": "22xx",

  5. "line_num": 1,

  6. "resource_members_id": "003",

  7. "subjectid": "10",

  8. "subjectcodecv": 5

  9. }, {

  10. "subjectname": "产品",

  11. "resource_members_name": "11xx",

  12. "line_num": 1,

  13. "resource_members_id": "004",

  14. "subjectid": "10",

  15. "subjectcodecv": 1

  16. }, {

  17. "subjectname": "公司",

  18. "resource_members_name": "xx",

  19. "line_num": 1,

  20. "resource_members_id": "005",

  21. "subjectid": "1001",

  22. "subjectcodecv": 3

  23. }

  24. ]

 line_num为2

resource_members_id也排好序了

 
  1. [

  2. {

  3. "subjectname": "产品",

  4. "resource_members_name": "22xx",

  5. "line_num": 2,

  6. "resource_members_id": "001",

  7. "subjectid": "10",

  8. "subjectcodecv": 3

  9. }, {

  10. "subjectname": "产品",

  11. "resource_members_name": "11xx",

  12. "line_num": 2,

  13. "resource_members_id": "002",

  14. "subjectid": "10",

  15. "subjectcodecv": 4

  16. }, {

  17. "subjectname": "公司",

  18. "resource_members_name": "xx",

  19. "line_num": 2,

  20. "resource_members_id": "003",

  21. "subjectid": "1001",

  22. "subjectcodecv": 5

  23. }

  24. ]

 

本文来源huayang183,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/18191

发表评论