JsonFormatConverter.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package com.kingdee.eas.custom.webbeisen.utils;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.List;
  7. public class JsonFormatConverter {
  8. /**
  9. * 转换 data 数组到指定字段的格式
  10. * @param originalJson 原始 JSON 字符串,格式为数组形式,可能包含 data 数组的对象
  11. * @param targetFields 需要转换的字段列表,由用户自定义
  12. * @return 转换后的 JSON 字符串
  13. */
  14. public static String convertDataFields(String originalJson, List<String> targetFields) {
  15. String fieldToRemove = "extfujian";
  16. originalJson = processSubCollection(originalJson, fieldToRemove);
  17. // 解析原始 JSON
  18. Object root = JSONObject.parse(originalJson);
  19. JSONArray dataArray = getRootDataArray(root);
  20. if (dataArray == null) {
  21. return originalJson; // 没有 data 数组时直接返回原始内容
  22. }
  23. // 处理 data 数组里的每个元素
  24. for (int i = 0; i < dataArray.size(); i++) {
  25. Object item = dataArray.get(i);
  26. if (item instanceof JSONObject) {
  27. processJsonObject((JSONObject) item, targetFields);
  28. }
  29. }
  30. return dataArray.toJSONString();
  31. }
  32. /**
  33. * 获取根数据数组(假设根对象为数组或包含 data 数组的对象)
  34. */
  35. private static JSONArray getRootDataArray(Object root) {
  36. if (root instanceof JSONArray) {
  37. return (JSONArray) root;
  38. } else if (root instanceof JSONObject) {
  39. return ((JSONObject) root).getJSONArray("data");
  40. }
  41. return null;
  42. }
  43. /**
  44. * 递归处理 JSONObject 并转换指定字段
  45. */
  46. private static void processJsonObject(JSONObject obj, List<String> targetFields) {
  47. // 处理当前对象中的目标字段
  48. for (String field : targetFields) {
  49. if (obj.containsKey(field)) {
  50. Object original = obj.get(field);
  51. if (original instanceof JSONArray) {
  52. // 将数组转换为键值对并直接添加到对象中
  53. convertArrayAndMerge(obj, field, (JSONArray) original);
  54. }
  55. }
  56. }
  57. // 递归处理子节点
  58. for (String key : obj.keySet()) {
  59. Object value = obj.get(key);
  60. if (value instanceof JSONObject) {
  61. processJsonObject((JSONObject) value, targetFields);
  62. } else if (value instanceof JSONArray) {
  63. processJsonArray((JSONArray) value, targetFields);
  64. }
  65. }
  66. }
  67. /**
  68. * 递归处理 JSONArray 并转换其中元素的指定字段
  69. */
  70. private static void processJsonArray(JSONArray array, List<String> targetFields) {
  71. for (int i = 0; i < array.size(); i++) {
  72. Object item = array.get(i);
  73. if (item instanceof JSONObject) {
  74. processJsonObject((JSONObject) item, targetFields);
  75. } else if (item instanceof JSONArray) {
  76. processJsonArray((JSONArray) item, targetFields);
  77. }
  78. }
  79. }
  80. /**
  81. * 将 JSONArray 转换为键值对并合并到父对象中
  82. */
  83. private static void convertArrayAndMerge(JSONObject parent, String originalField, JSONArray array) {
  84. if (array == null) return;
  85. // 移除原始字段
  86. parent.remove(originalField);
  87. // 处理数组中的每个元素
  88. for (int i = 0; i < array.size(); i++) {
  89. Object item = array.get(i);
  90. if (item instanceof JSONObject) {
  91. JSONObject jsonItem = (JSONObject) item;
  92. String primaryKey = extractNameField(jsonItem);
  93. if (primaryKey != null) {
  94. // 创建一个不包含 name 字段的新对象
  95. JSONObject processedItem = new JSONObject(true);
  96. for (String key : jsonItem.keySet()) {
  97. if (!"name".equals(key)) {
  98. processedItem.put(key, jsonItem.get(key));
  99. }
  100. }
  101. // 直接将处理后的对象添加到父对象中
  102. parent.put(primaryKey, processedItem);
  103. }
  104. }
  105. }
  106. }
  107. /**
  108. * 获取对象中的 name 字段值
  109. */
  110. private static String extractNameField(JSONObject obj) {
  111. if (obj.containsKey("name")) {
  112. Object nameValue = obj.get("name");
  113. return nameValue != null ? nameValue.toString() : null;
  114. }
  115. return null;
  116. }
  117. /**
  118. * 处理 subCollection 数据,去除指定 name 的字段
  119. * @param originalJson 原始 JSON 字符串
  120. * @param fieldToRemove 需要移除的字段名
  121. * @return 处理后的 JSON 字符串
  122. */
  123. public static String processSubCollection(String originalJson, String fieldToRemove) {
  124. // 解析原始 JSON
  125. Object root = JSONObject.parse(originalJson);
  126. JSONArray rootArray = getRootArray(root);
  127. if (rootArray == null) {
  128. return originalJson; // 没有根数组时直接返回原始内容
  129. }
  130. // 处理每个根数组元素
  131. for (int i = 0; i < rootArray.size(); i++) {
  132. Object item = rootArray.get(i);
  133. if (item instanceof JSONObject) {
  134. JSONObject jsonItem = (JSONObject) item;
  135. processSubCollectionObject(jsonItem, fieldToRemove);
  136. }
  137. }
  138. return rootArray.toJSONString();
  139. }
  140. /**
  141. * 获取根数组
  142. */
  143. private static JSONArray getRootArray(Object root) {
  144. if (root instanceof JSONArray) {
  145. return (JSONArray) root;
  146. }
  147. return null;
  148. }
  149. /**
  150. * 处理包含键名包含 subCollection 的 JSONObject
  151. */
  152. private static void processSubCollectionObject(JSONObject obj, String fieldToRemove) {
  153. for (String key : obj.keySet()) {
  154. if (key.contains("subCollection")) {
  155. Object value = obj.get(key);
  156. if (value instanceof JSONArray) {
  157. JSONArray subCollections = (JSONArray) value;
  158. for (int i = 0; i < subCollections.size(); i++) {
  159. Object subItem = subCollections.get(i);
  160. if (subItem instanceof JSONObject) {
  161. JSONObject subObj = (JSONObject) subItem;
  162. JSONArray fields = subObj.getJSONArray("fields");
  163. if (fields != null) {
  164. removeField(fields, fieldToRemove);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. /**
  173. * 从 fields 中移除指定 name 的字段
  174. */
  175. private static void removeField(JSONArray fields, String fieldToRemove) {
  176. List<Integer> indicesToRemove = new ArrayList<Integer>();
  177. for (int i = 0; i < fields.size(); i++) {
  178. Object fieldItem = fields.get(i);
  179. if (fieldItem instanceof JSONObject) {
  180. JSONObject fieldObj = (JSONObject) fieldItem;
  181. String fieldName = fieldObj.getString("name");
  182. if (fieldName.indexOf(fieldToRemove)>=0) {
  183. indicesToRemove.add(i);
  184. }
  185. }
  186. }
  187. // 从后往前移除元素,避免索引错乱
  188. for (int i = indicesToRemove.size() - 1; i >= 0; i--) {
  189. int index = indicesToRemove.get(i);
  190. fields.remove(index);
  191. }
  192. }
  193. }