【问题标题】:Splitting JSON string into multiple JSON strings in Java在Java中将JSON字符串拆分为多个JSON字符串
【发布时间】:2017-01-17 07:20:45
【问题描述】:

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

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

paths 内可能有可变数量的MyPath 键。我想把这个结构分解成下面这样:

  • 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 中有什么简单的方法可供我选择吗?

【问题讨论】:

  • 您可以像这样删除部分 json 对象 delete arr.paths.MyPath2;因此,将您的 json 添加到两个 var 中,并从第一个删除第一个 mypath,从第二个删除第二个我的路径。 jsfiddle.net/h20voga5如果我没记错的话,这就是你需要的
  • 感谢您的回复.. 好主意,但问题是我不知道我的paths 里面会有多少Mypath。我在想的是把 JSON 放在一个 @ 987654330@ 并从 JSON 中删除 paths 键并将其存储在单独的 JSONObject 中并迭代此第二个对象并将 Mypath 添加到第一个 json 中。但问题是我的JSONObject 无法识别MyPath12 以及我的第二个json 中的Keyset 值.. 希望我没有混淆你;)
  • 哦,我明白了,我在 json 上做了很多工作,我不知道里面有什么以及有多少对象,你可以检查我回答的这个问题,它从 json 自动生成 html (@ 987654322@),您应该只检查路径的长度,并为其执行 while 循环,并生成新的 JSON 对象,它会为每个将具有拆分路径的 myPath 删除(检查索引并使用 object.keys 获取 objName)。如果你不设法做到这一点,我会在下班后尝试制作 JSfiddle :)

标签: java json


【解决方案1】:

在以下几行尝试一些东西..不是完美的,但一个开始..

  1. 将 Json 转换为 Java 对象。您可能需要一个 java 类或 Pojo 来映射到它(除非您已经有一个)。 变量我的应用程序; var 列表列表;

  2. 将 Java 对象转换回所需格式的 Json 对象。阅读每一项

    • 始终添加 MyApp 遍历 List 列表 添加到 JsonObjectCollection
  3. 返回所有对象并根据需要使用它们

【讨论】:

    【解决方案2】:

    编辑(只是注意到它在 Java 中需要,我的错) - 可能有更简单的方法,但这是我得到的解决方案。 当您导入 JSON 时,将其保存到 2 个 vars ,其中一个将用于创建新对象。 为所有新对象创建 newArr。 我正在遍历所有路径并创建保存在 newObjectsArray 中的 newPath,然后推送到 newArr。 然后您可以通过该数组访问所有对象。 如果您有多个 myApps ,您可以制作 $each 它将获取上层数据并创建新的 json,然后在其中移动 while 循环

    jsFiddle:https://jsfiddle.net/h20voga5/2/

          var arr = {
     "MyApp":"2.0",
       "info":{  
          "version":"1.0.0"
       },
       "paths":{  
          "MyPath1":{
          "Key":"Value1"
          },
          "MyPath2":{
          "Key":"Value2"
          }
    }
    }
    
    var newObjectArray = {
     "MyApp":"2.0",
       "info":{  
          "version":"1.0.0"
       },
       "paths":{  
          "MyPath1":{
          "Key":"Value1"
          },
          "MyPath2":{
          "Key":"Value2"
          }
    }
    }
    
    var newArr = {'Data':[]}  
    var length = Object.keys(arr.paths).length; 
    var i = 0
    
    
    while(i < length){
     newObjectArray.paths = {} 
      var objectKey = Object.keys(arr.paths)[i] // MyPath1, MyPath2...
      var objectValues = JSON.stringify(arr.paths[objectKey]) // {"Key":"Value1"} 
      var newPath = '{'+ objectKey + ':' +  objectValues+'}'
    
      newObjectArray.paths = newPath 
      newArr.Data.push(newObjectArray)
    
    
      i++
    }
    
    
    var countObjects = newArr.Data.length
    alert(countObjects)
    alert(JSON.stringify(newArr.Data[0])) // access your objects like this 
    

    【讨论】:

    • 我知道 OP 想用 Java 来做。
    • 我刚注意到,好吧,我给了他一些逻辑来解决问题 :) 我的错
    【解决方案3】:

    使用Jackson,一种流行的Java JSON 解析器,您可以拥有以下功能:

    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。

    【讨论】:

    • 谢谢。如果我想探索这个杰克逊的更多功能,有什么好的方法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多