在Java中将JSON字符串拆分为多个JSON字符串 [英] Splitting JSON string into multiple JSON strings in Java

查看:570
本文介绍了在Java中将JSON字符串拆分为多个JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于下面的JSON结构:

I have got a JSON structure similar to below:

{
  "MyApp": "2.0",
  "info": {
    "version": "1.0.0"
  },
  "paths": {
    "MyPath1": {
      "Key": "Value"
    },
    "MyPath2": {
      "Key": "Value"
    }
  }
}

paths中,可以有可变数量的MyPath键.我想将此结构分解为如下所示:

Within paths there could be a variable number of MyPath keys. I want to break this structure into something like below:

  • JSON 1:
{
  "MyApp": "2.0",
  "info": {
    "version": "1.0.0"
  },
  "paths": {
    "MyPath1": {
      "Key": "Value"
    }
  }
}

  • JSON 2:
  • {
      "MyApp": "2.0",
      "info": {
        "version": "1.0.0"
      },
      "paths": {
        "MyPath2": {
          "Key": "Value"
        }
      }
    }
    

    在Java中,我可以选择任何简单的方法吗?

    Is there any simple approach I can opt for this in Java?

    推荐答案

    有了Jackson,Java的流行JSON解析器,您可以拥有以下内容:

    With Jackson, a popular JSON parser for Java, you can have the following:

    String json = "{\"MyApp\":\"2.0\",\"info\":{\"version\":\"1.0.0\"},\"paths\":"
                + "{\"MyPath1\":{\"Key\":\"Value\"},\"MyPath2\":{\"Key\":\"Value\"}}}";
    
    // Create an ObjectMapper instance the manipulate the JSON
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    
    // Create a list to store the result (the list will store Jackson tree model objects)
    List<JsonNode> result = new ArrayList<>();
    
    // Read the JSON into the Jackson tree model and get the "paths" node
    JsonNode tree = mapper.readTree(json);
    JsonNode paths = tree.get("paths");
    
    // Iterate over the field names under the "paths" node
    Iterator<String> fieldNames = paths.fieldNames();
    while (fieldNames.hasNext()) {
    
        // Get a child of the "paths" node
        String fieldName = fieldNames.next();
        JsonNode path = paths.get(fieldName);
    
        // Create a copy of the tree
        JsonNode copyOfTree = mapper.valueToTree(tree);
    
        // Remove all the children from the "paths" node; add a single child to "paths"
        ((ObjectNode) copyOfTree.get("paths")).removeAll().set(fieldName, path);
    
        // Add the modified tree to the result list
        result.add(copyOfTree);
    }
    
    // Print the result
    for (JsonNode node : result) {
        System.out.println(mapper.writeValueAsString(node));
        System.out.println();
    }
    

    输出为:

    {
      "MyApp" : "2.0",
      "info" : {
        "version" : "1.0.0"
      },
      "paths" : {
        "MyPath1" : {
          "Key" : "Value"
        }
      }
    }
    
    {
      "MyApp" : "2.0",
      "info" : {
        "version" : "1.0.0"
      },
      "paths" : {
        "MyPath2" : {
          "Key" : "Value"
        }
      }
    }
    

    将为paths节点的每个子节点创建一个新的JSON.

    A new JSON will be created for each child of the paths node.

    这篇关于在Java中将JSON字符串拆分为多个JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆