将JSON字符串拆分为Java中的多个JSON字符串

时间:2016-09-09 07:52:14

标签: java json

我有一个类似于下面的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中选择这个?

3 个答案:

答案 0 :(得分:2)

使用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。

答案 1 :(得分:0)

尝试以下几行......不完美但不是开始......

  1. 将Json转换为Java对象。您可能需要一个java类或Pojo来映射它(除非您已经有一个)。 Var MyApp; var List list;

  2. 以所需格式将Java对象转换回Json对象。阅读每个项目

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

答案 2 :(得分:0)

编辑(刚刚注意到它需要Java,我的不好) - 可能有更简单的方法,但这是我得到的解决方案。 导入JSON时将其保存为2个变量,其中一个将用于创建新对象。 为所有新对象创建newArr。 我循环遍历所有路径并创建newPath,它保存在newObjectsArray中,然后推送到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 
相关问题