我正在尝试将json数据放入HashMap中,其中可能包含很多重复的键。 例如:
{
"member Detial": {
"firstname": "jhon",
"lastName": "wick",
"Address": "New York",
"Email": "jhonwick@boogiman.com",
"Fax Number": "09876543",
"Contact Number": "9876543210",
"Password": "newyorkcitizen",
"confirm password": "newyorkcitizen",
"NationalProviderIdentifier": "Apollo",
"PartyID": "1"
},
"Physician": {
"firstname": "jhon",
"lastName": "wick",
"Email": "jhonwick@boogiman.com",
"Fax Number": "09876543",
"Contact Number": "9876543210",
"NationalProviderIdentifier": "Apollo",
"PartyID": "1"
},
"PhysicianGroup": {
"Address": "New York",
"Email": "jhonwick@boogiman.com",
"Fax Number": "09876543",
"Contact Number": "9876543210",
"NationalProviderIdentifier": "Apollo",
"Hospital name": "Apollo",
"Tax ID": "12345",
"PartyID": "1"
},
"Physician": {
"Address": "New York",
"Email": "jhonwick@boogiman.com",
"Fax Number": "09876543",
"Contact Number": "9876543210",
"NationalProviderIdentifier": "Apollo",
"Hospital name": "Apollo",
"Tax ID": "12345",
"PartyID": "1"
},
}
上面的json文件中的被认为是“成员详细信息”,而括号内的数据被视为值。但是这里的问题是上面的json示例中可能有重复的键,例如“ Physician”。
我想将第二个“医师”的值添加到第一个“医师”而不覆盖其值,并删除重复键。 例如:` {
"member Detial": {
"firstname": "jhon",
"lastName": "wick",
"Address": "New York",
"Email": "jhonwick@boogiman.com",
"Fax Number": "09876543",
"Contact Number": "9876543210",
"Password": "newyorkcitizen",
"confirm password": "newyorkcitizen",
"NationalProviderIdentifier": "Apollo",
"PartyID": "1"
},
"Physician": [{
"firstname": "jhon",
"lastName": "wick",
"Email": "jhonwick@boogiman.com",
"Fax Number": "09876543",
"Contact Number": "9876543210",
"NationalProviderIdentifier": "Apollo",
"PartyID": "1"
},
{
"Address": "New York",
"Email": "jhonwick@boogiman.com",
"Fax Number": "09876543",
"Contact Number": "9876543210",
"NationalProviderIdentifier": "Apollo",
"Hospital name": "Apollo",
"Tax ID": "12345",
"PartyID": "1"
}
],
"PhycianGroup": {
"Address": "New York",
"Email": "jhonwick@boogiman.com",
"Fax Number": "09876543",
"Contact Number": "9876543210",
"NationalProviderIdentifier": "Apollo",
"Hospital name": "Apollo",
"Tax ID": "12345",
"PartyID": "1"
}
}
`
答案 0 :(得分:2)
Guava与Multimap<K, V>
一起提供。这正是您要寻找的:
将键映射到值的集合,类似于
Map
,但其中每个键可能与多个值关联。您可以将多图的内容可视化为从键到值的非空集合的映射:
- a→1,2
- b→3
...或作为键值对的单个“扁平化”集合:
- a→1
- a→2
- b→3
如果您想使用Jackson(一种流行的Java JSON解析器),请执行以下操作:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new GuavaModule());
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
Multimap<String, Object> data =
mapper.readValue(json, new TypeReference<Multimap<String, Object>>() {});
一些相关详细信息:
GuavaModule
是杰克逊datatype collection module,用于支持Guava类型。
通过启用ACCEPT_SINGLE_VALUE_AS_ARRAY
,Jackson将执行从非JSON数组值到单元素数组的自动转换,这是将JSON解析为Multimap<K, V>
所必需的。
以下依赖项是必需的:
<!-- Guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<!-- Jackson Core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- Jackson Databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- Jackson module for Guava integration -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>${jackson.version}</version>
</dependency>
要将地图转换回JSON文档,请使用:
String output = mapper.writeValueAsString(data);
答案 1 :(得分:1)
首先检查map
是否已经包含该密钥,如果是,则添加它。
如果您使用的是类似HashMap<String, List<YourObject>>
的内容:
if (map.containsKey(key)) {
map.get(key).add(yourObject);
}