简体   繁体   English

如何解析具有字符串键和多个相同键实例的java中的给定json?

[英]How to parse a given json in java which has a string key with multiple identical key instances?

My Question seems very similar to this question , but what happens if the are duplicate values in the json file? 我的问题似乎与此问题非常相似,但是如果json文件中的重复值会发生什么呢?

The duplicate values are found in the json file due to the file contents originating from postgres which allow to insert duplicate values in older JSON format files. 由于文件内容源自postgres,因此可以在json文件中找到重复值,从而可以在较早的JSON格式文件中插入重复值。

My input look like this. 我的输入看起来像这样。

 {
"61":{"value":5,"to_value":5},
"58":{"r":0,"g":0,"b":255}, "58":{"r":165,"g":42,"b":42},"58:{"r":0,"g":255,"b":0},
"63":{"r":0,"g":0,"b":0},
"57":{"r":0,"g":0,"b":255},"57":{"r":0,"g":255,"b":0}
}

If you look carefully there are multiple values of "58" as keys. 如果仔细看,有多个“ 58”值作为键。 The main keys "61" and "58" are mappted to a nested map type with different keys. 主键“ 61”和“ 58”映射到具有不同键的嵌套映射类型。

Now to simplify what I want to achieve, my output of the above input json should look like this. 现在,为了简化我要实现的目标,上述输入json的输出应如下所示。

Approach or solution both equally appreciated in java only. 仅在Java中同样赞赏方法或解决方案。

 {
"61":[5,5],
"58": [{"r":0,"g":0,"b":255},{"r":165,"g":42,"b":42},{"r":0,"g":255,"b":0}],
"63":[{"r":0,"g":0,"b":0}],
"57":[{"r":0,"g":0,"b":255},{"r":0,"g":255,"b":0}]
}

A good tool for parsing JSON formats is this library: JSONObject 这个库是解析JSON格式的好工具: JSONObject

Here an example of usage in a previous SO question: Parsing JSON which contains duplicate keys 这是上一个SO问题的用法示例: 解析包含重复键的JSON

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM