| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- package com.kingdee.eas.custom.webbeisen.utils;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class JsonFormatConverter {
- /**
- * 转换 data 数组到指定字段的格式
- * @param originalJson 原始 JSON 字符串,格式为数组形式,可能包含 data 数组的对象
- * @param targetFields 需要转换的字段列表,由用户自定义
- * @return 转换后的 JSON 字符串
- */
- public static String convertDataFields(String originalJson, List<String> targetFields) {
- String fieldToRemove = "extfujian";
- originalJson = processSubCollection(originalJson, fieldToRemove);
- // 解析原始 JSON
- Object root = JSONObject.parse(originalJson);
- JSONArray dataArray = getRootDataArray(root);
- if (dataArray == null) {
- return originalJson; // 没有 data 数组时直接返回原始内容
- }
- // 处理 data 数组里的每个元素
- for (int i = 0; i < dataArray.size(); i++) {
- Object item = dataArray.get(i);
- if (item instanceof JSONObject) {
- processJsonObject((JSONObject) item, targetFields);
- }
- }
- return dataArray.toJSONString();
- }
- /**
- * 获取根数据数组(假设根对象为数组或包含 data 数组的对象)
- */
- private static JSONArray getRootDataArray(Object root) {
- if (root instanceof JSONArray) {
- return (JSONArray) root;
- } else if (root instanceof JSONObject) {
- return ((JSONObject) root).getJSONArray("data");
- }
- return null;
- }
- /**
- * 递归处理 JSONObject 并转换指定字段
- */
- private static void processJsonObject(JSONObject obj, List<String> targetFields) {
- // 处理当前对象中的目标字段
- for (String field : targetFields) {
- if (obj.containsKey(field)) {
- Object original = obj.get(field);
- if (original instanceof JSONArray) {
- // 将数组转换为键值对并直接添加到对象中
- convertArrayAndMerge(obj, field, (JSONArray) original);
- }
- }
- }
- // 递归处理子节点
- for (String key : obj.keySet()) {
- Object value = obj.get(key);
- if (value instanceof JSONObject) {
- processJsonObject((JSONObject) value, targetFields);
- } else if (value instanceof JSONArray) {
- processJsonArray((JSONArray) value, targetFields);
- }
- }
- }
- /**
- * 递归处理 JSONArray 并转换其中元素的指定字段
- */
- private static void processJsonArray(JSONArray array, List<String> targetFields) {
- for (int i = 0; i < array.size(); i++) {
- Object item = array.get(i);
- if (item instanceof JSONObject) {
- processJsonObject((JSONObject) item, targetFields);
- } else if (item instanceof JSONArray) {
- processJsonArray((JSONArray) item, targetFields);
- }
- }
- }
- /**
- * 将 JSONArray 转换为键值对并合并到父对象中
- */
- private static void convertArrayAndMerge(JSONObject parent, String originalField, JSONArray array) {
- if (array == null) return;
- // 移除原始字段
- parent.remove(originalField);
- // 处理数组中的每个元素
- for (int i = 0; i < array.size(); i++) {
- Object item = array.get(i);
- if (item instanceof JSONObject) {
- JSONObject jsonItem = (JSONObject) item;
- String primaryKey = extractNameField(jsonItem);
- if (primaryKey != null) {
- // 创建一个不包含 name 字段的新对象
- JSONObject processedItem = new JSONObject(true);
- for (String key : jsonItem.keySet()) {
- if (!"name".equals(key)) {
- processedItem.put(key, jsonItem.get(key));
- }
- }
- // 直接将处理后的对象添加到父对象中
- parent.put(primaryKey, processedItem);
- }
- }
- }
- }
- /**
- * 获取对象中的 name 字段值
- */
- private static String extractNameField(JSONObject obj) {
- if (obj.containsKey("name")) {
- Object nameValue = obj.get("name");
- return nameValue != null ? nameValue.toString() : null;
- }
- return null;
- }
- /**
- * 处理 subCollection 数据,去除指定 name 的字段
- * @param originalJson 原始 JSON 字符串
- * @param fieldToRemove 需要移除的字段名
- * @return 处理后的 JSON 字符串
- */
- public static String processSubCollection(String originalJson, String fieldToRemove) {
- // 解析原始 JSON
- Object root = JSONObject.parse(originalJson);
- JSONArray rootArray = getRootArray(root);
- if (rootArray == null) {
- return originalJson; // 没有根数组时直接返回原始内容
- }
- // 处理每个根数组元素
- for (int i = 0; i < rootArray.size(); i++) {
- Object item = rootArray.get(i);
- if (item instanceof JSONObject) {
- JSONObject jsonItem = (JSONObject) item;
- processSubCollectionObject(jsonItem, fieldToRemove);
- }
- }
- return rootArray.toJSONString();
- }
- /**
- * 获取根数组
- */
- private static JSONArray getRootArray(Object root) {
- if (root instanceof JSONArray) {
- return (JSONArray) root;
- }
- return null;
- }
- /**
- * 处理包含键名包含 subCollection 的 JSONObject
- */
- private static void processSubCollectionObject(JSONObject obj, String fieldToRemove) {
- for (String key : obj.keySet()) {
- if (key.contains("subCollection")) {
- Object value = obj.get(key);
- if (value instanceof JSONArray) {
- JSONArray subCollections = (JSONArray) value;
- for (int i = 0; i < subCollections.size(); i++) {
- Object subItem = subCollections.get(i);
- if (subItem instanceof JSONObject) {
- JSONObject subObj = (JSONObject) subItem;
- JSONArray fields = subObj.getJSONArray("fields");
- if (fields != null) {
- removeField(fields, fieldToRemove);
- }
- }
- }
- }
- }
- }
- }
- /**
- * 从 fields 中移除指定 name 的字段
- */
- private static void removeField(JSONArray fields, String fieldToRemove) {
- List<Integer> indicesToRemove = new ArrayList<Integer>();
- for (int i = 0; i < fields.size(); i++) {
- Object fieldItem = fields.get(i);
- if (fieldItem instanceof JSONObject) {
- JSONObject fieldObj = (JSONObject) fieldItem;
- String fieldName = fieldObj.getString("name");
- if (fieldName.indexOf(fieldToRemove)>=0) {
- indicesToRemove.add(i);
- }
- }
- }
- // 从后往前移除元素,避免索引错乱
- for (int i = indicesToRemove.size() - 1; i >= 0; i--) {
- int index = indicesToRemove.get(i);
- fields.remove(index);
- }
- }
- }
|