簡體   English   中英

基於給定鍵從 JSONArray 嵌套 JSONobjects

[英]Nesting of JSONobjects from a JSONArray based on given keys

我正在嘗試將 JSONArray 轉換為“分組” json object 格式。

可以有 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
  }
]

預期 output 格式

  "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
            }
          ]
        }
      ]
    }
  ]
}

我在開始之前考慮的要點

  • 只有最里面的 object 才會有計數
  • 我需要一個 map 來引用我之前遇到的 object
  • 對於 JSONArray 中的每個 object,我必須遍歷可用字段

我試過的示例代碼

    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 來解決這個問題,使用現有的嵌套對象或創建它們以防它們尚不存在。

請注意,此代碼尚未經過測試,其性能可能會得到改善。

public static JSONObject process(JSONArray dataArray, List<Field> fieldList) throws JSONException {
    JSONObject result = new JSONObject();

    for (int i = 0; i < dataArray.length(); i++) {
        JSONObject element = dataArray.getJSONObject(i);
        JSONObject currentParent = result;

        for (Field field : fieldList) {
            String columnName = field.getColumnName();
            int id = element.getInt(columnName);

            JSONArray children = currentParent.optJSONArray(columnName);
            if (children == null) {
                children = new JSONArray();
                currentParent.put(columnName, children);
            }

            // Find the child with matching `id`
            JSONObject child = null;
            for (int childIndex = 0; childIndex < children.length(); childIndex++) {
                child = children.getJSONObject(childIndex);
                if (child.getInt("id") == id) {
                    break;
                }
            }
            // No matching child found, create new one
            if (child == null) {
                child = new JSONObject();
                child.put("id", id);
                children.put(child);
            }
            // Update parent
            currentParent = child;
        }

        // The most nested object should store the `count`
        currentParent.put("count", element.getInt("count"));
    }

    return result;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM