1

在 java 中,我试图将嵌套 JSON 转换为平面 JSON。下面是输入有效负载。

输入有效载荷:

{
  "root": {
    "data": {
      "a": "1",
      "b": {
        "c": "2"
      },
      "test": [
        {
          "e": "3"
        },
        {
          "f": "4"
        }
      ],
      "test1": [
        {
          "g": "5"
        }
      ],
      "test2": [
        1,
        2,
        3
      ]
    }
  }
}

从上面的有效负载中,我试图转换为以下扁平化的 JSON。

{
 "root.data.a":"1",
 "root.data.b.c":"2",
 "root.data.test[]":[
        {
          "e": "3"
        },
        {
          "f": "4"
        }
      ],
  "root.data.test1[].g":"5",
  "root.data.test2":[1,2,3]
}

在上述 JSON 中,转换为平面 JSON 时使用了以下条件。

  1. 如果 JSONObject 包含嵌套的 JSONObject 和值作为除 JSONArray 之外的任何数据类型,则平面 JSON 的键和值必须"root.data.a"为 1,值必须为 1。

  2. 如果 JSONObject 由 JSONArray 组成,并且 JSONArray 的长度大于 1,则平面 JSON 的键必须是"root.data.test[]"完整的 JSONArray,值必须是完整的 JSONArray。

  3. 如果 JSONObject 由 JSONArray 组成,并且 JSONArray 的长度为 1,则平面 JSON 必须 key 必须由 JSON 数组中的所有 JSON object 元素组成,"root.data.test1[].g"并且 value 为“5”。

  4. 如果 JSON 键的值为 Array 且没有任何 JSON 对象,则将值分配给平面 JSON 中的该路径,如键 as"root.data.test2"和值 as[1,2,3]

那么,如何从输入 JSON 转换为平面 JSON 。

4

2 回答 2

1

这将有助于https://github.com/wnameless/json-flattener它是一个 Java 实用程序,用于 FLATTEN 嵌套的 JSON 对象,甚至更多地用于 UNFLATTEN 它回来。

于 2020-04-03T08:26:06.113 回答
1
public class Sample {
    public static void main(String[] args) {
        Sample a= new Sample();
        a.testCreatingKeyValues();
    }

    String json = 
        "{\n  \"root\": {\n    \"data\": {\n      \"a\": \"1\",\n      \"b\": {\n        \"c\": \"2\"\n      },\n      \"test\": [\n        {\n          \"e\": \"3\"\n        },\n        {\n          \"f\": \"4\"\n        }\n      ],\n      \"test1\": [\n        {\n          \"g\": \"5\"\n        }\n      ],\n      \"test2\": [\n        1,\n        2,\n        3\n      ]\n    }\n  }\n}";

        //  @Test
          public void testCreatingKeyValues() {
            Map<String, Object> map = new HashMap<String, Object>();
            try {
              addKeys("", new ObjectMapper().readTree(json), map);
            } catch (IOException e) {
              e.printStackTrace();
            }
            System.out.println(map);
          }

          private void addKeys(String currentPath, JsonNode jsonNode, Map<String, Object> map) {
            if (jsonNode.isObject()) {
              ObjectNode objectNode = (ObjectNode) jsonNode;
              Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields();
              String pathPrefix = currentPath.isEmpty() ? "" : currentPath + ".";

              while (iter.hasNext()) {
                Map.Entry<String, JsonNode> entry = iter.next();
                addKeys(pathPrefix + entry.getKey(), entry.getValue(), map);
              }
            } else if (jsonNode.isArray()) {
              ArrayNode arrayNode = (ArrayNode) jsonNode;
              map.put(currentPath+"[]", arrayNode);

            } else if (jsonNode.isValueNode()) {
              ValueNode valueNode = (ValueNode) jsonNode;
              map.put(currentPath, valueNode.asText());
            }
          }
}

给出输出:

{root.data.b.c=2, root.data.test2[]=[1,2,3], root.data.test1[]=[{"g":"5"}], root.data.a=1, root.data.test[]=[{"e":"3"},{"f":"4"}]}
于 2020-04-03T09:34:07.173 回答