Java 基于给定键从JSONArray嵌套JSONObject

Java 基于给定键从JSONArray嵌套JSONObject,java,json,Java,Json,我正在尝试将JSONArray转换为“分组”json对象格式 可以有N个列,由C1、C2、C3表示 在此,数据按顺序-C1、C2、C3分组 样本JSONArray [ { "C1": 1001, "C2": 2001, "C3": 3001, "count": 10 }, { "C1": 1001, "C2": 200

我正在尝试将JSONArray转换为“分组”json对象格式

可以有N个列,由C1、C2、C3表示

在此,数据按顺序-C1、C2、C3分组

样本JSONArray

[
  {
    "C1": 1001,
    "C2": 2001,
    "C3": 3001,
    "count": 10
  },
  {
    "C1": 1001,
    "C2": 2001,
    "C3": 3001,
    "count": 8
  },
  {
    "C1": 1001,
    "C2": 2001,
    "C3": 3003,
    "count": 5
  },
  {
    "C1": 1001,
    "C2": 2002,
    "C3": 3001,
    "count": 5
  },
  {
    "C1": 1002,
    "C2": 2002,
    "C3": 3001,
    "count": 9
  },
  {
    "C1": 1002,
    "C2": 2002,
    "C3": 3002,
    "count": 3
  },
  {
    "C1": 1003,
    "C2": 2001,
    "C3": 3001,
    "count": 2
  }
]
预期输出格式

  "C1": [
    {
      "id": 1001,
      "C2": [
        {
          "id": 2001,
          "C3": [
            {
              "id": 3001,
              "count": 10
            },
            {
              "id": 3002,
              "count": 8
            },
            {
              "id": 3003,
              "count": 5
            }
          ]
        },
        {
          "id": 2002,
          "C3": [
            {
              "id": 3001,
              "count": 5
            }
          ]
        }
      ]
    },
    {
      "id": 1002,
      "C2": [
        {
          "id": 2002,
          "C3": [
            {
              "id": 3001,
              "count": 9
            },
            {
              "id": 3002,
              "count": 3
            }
          ]
        }
      ]
    },
    {
      "id": 1003,
      "C2": [
        {
          "id": 2001,
          "C3": [
            {
              "id": 3001,
              "count": 2
            }
          ]
        }
      ]
    }
  ]
}
我在开始之前考虑过的几点

  • 只有最里面的对象才会有计数
  • 我需要一个映射来引用我之前遇到的对象
  • 对于JSONArray中的每个对象,我都必须遍历可用字段
我试过的示例代码

    public static JSONObject process(JSONArray dataArray, List<Field> fieldList) {
        // To save the ID vs JSONArray to put later
        Map<String, JSONObject> idObjectMap = new HashMap<>();
        
        int numberOfFields = fieldList.size();
        JSONObject returnObject = new JSONObject();
        JSONArray finalArray = new JSONArray();
        for (int i = 0; i < dataArray.length(); i++) {
            JSONObject rowObject = dataArray.getJSONObject(i);
            for (int j = 0; j < numberOfFields; j++) {
                Field field = fieldList.get(j);
                // getColumnName() will return C1/C2/C3 ...
                String id = rowObject.get(field.getColumnName()).toString();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", id);


                // Only the inner most object will have the count
                if (j + 1 == numberOfFields) {
                    int count = rowObject.getInt("count");
                    jsonObject.put("count", count);

                }
                idObjectMap.put(id, jsonObject);

            }
        }
        
        return returnObject;
    }
公共静态JSONObject进程(JSONArray dataArray,List fieldList){
//保存ID vs JSONArray以稍后放置
Map idObjectMap=新HashMap();
int numberofields=fieldList.size();
JSONObject returnObject=新的JSONObject();
JSONArray finalArray=新的JSONArray();
对于(int i=0;i
问题:


我不知道接下来该怎么办。我试图切换循环和过程,但似乎很难将先前形成的对象保留在引用中。我在嵌套对象时也遇到了问题。如果有人能给我一些指导或提示,那将很有帮助。

您可以通过动态构造结果JSONObject、使用现有嵌套对象或在它们还不存在的情况下创建它们来解决这个问题

请注意,此代码尚未测试,其性能可能会得到改进

公共静态JSONObject进程(JSONArray dataArray,List fieldList)抛出JSONException{
JSONObject结果=新建JSONObject();
对于(int i=0;i
您可以通过动态构造结果JSONObject来解决这个问题,使用现有的嵌套对象,或者在它们还不存在的情况下创建它们

请注意,此代码尚未测试,其性能可能会得到改进

公共静态JSONObject进程(JSONArray dataArray,List fieldList)抛出JSONException{
JSONObject结果=新建JSONObject();
对于(int i=0;i