Java 将具有重复键的JSON表示为多重映射
我有一个带有重复键的JSON,如下所示Java 将具有重复键的JSON表示为多重映射,java,json,jackson,multimap,Java,Json,Jackson,Multimap,我有一个带有重复键的JSON,如下所示 { "name": "Test", "attributes": [{ "attributeName": "One", "attributeName": "Two", "attributeName": "Three" }] } 当我使用Jackson将其转换为贴图时,其转换如下所示 { "name"
{
"name": "Test",
"attributes": [{
"attributeName": "One",
"attributeName": "Two",
"attributeName": "Three"
}]
}
当我使用Jackson将其转换为贴图时,其转换如下所示
{
"name": "Test",
"attributes": [{
"attributeName": "One",
"attributeName": "Two",
"attributeName": "Three"
}]
}
{name=Test,attributes=[{attributeName=Three}]}
将考虑属性名称最后一次出现的值。有没有办法让Jackson把它作为一个替代品?我可以使用Multimap的任何实现。我当前的代码如下所示:
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
public class TestJSON {
public static void main(String[] args) throws Exception{
ObjectMapper mapper = new ObjectMapper();
String json = "{\"name\": \"Test\",\"attributes\": [{\"attributeName\": \"One\",\"attributeName\": \"Two\",\"attributeName\": \"Three\"}]}";
Map<String, Object> map = new HashMap<>();
map = mapper.readValue(json, new TypeReference<Map<String, Object>>(){});
System.out.println(map);
}
}
import java.util.HashMap;
导入java.util.Map;
导入com.fasterxml.jackson.core.type.TypeReference;
导入com.fasterxml.jackson.databind.ObjectMapper;
公共类TestJSON{
公共静态void main(字符串[]args)引发异常{
ObjectMapper mapper=新的ObjectMapper();
字符串json=“{\'name\':\'Test\',\'attributes\':[{\'attributeName\':\'One\',\'attributeName\':\'Two\',\'attributeName\':\'Three\'}];
Map Map=newhashmap();
map=mapper.readValue(json,newTypeReference(){});
系统输出打印项次(map);
}
}
不要认为Jackson会以他自己的方式处理它,但是您可以将这个JSON包装到简单的POJO中,并从中获得多映射。例如:
public class Attribute {
private String attributeName;
// getter and setter here
}
public class AttributeContainer {
private String name;
private List<Attribute> attributes;
public Multimap<String, String> getAttributeMultiMap() {
ImmutableMultimap.Builder<String, String> builder = ImmutableMultimap.builder();
for (Attribute attribute : attributes) {
builder.put("attributeName", attribute.getAttributeName())
}
return builder.build();
}
// getters and setters here
}
public void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
String json = "{\"name\": \"Test\",\"attributes\": [{\"attributeName\": \"One\",\"attributeName\": \"Two\",\"attributeName\": \"Three\"}]}";
AttributeContainer attributeContainer;
attributeContainer = mapper.readValue(json, new TypeReference<AttributeContainer>(){});
System.out.println(attributeContainer.getAttributeMultiMap());
}
公共类属性{
私有字符串attributeName;
//这里是盖特和塞特
}
公共类属性容器{
私有字符串名称;
私有列表属性;
公共多映射getAttributeMultiMap(){
ImmutableMultimap.Builder=ImmutableMultimap.Builder();
用于(属性:属性){
put(“attributeName”,attribute.getAttributeName())
}
返回builder.build();
}
//这里有接球手和接球手
}
公共void main(字符串[]参数){
ObjectMapper mapper=新的ObjectMapper();
字符串json=“{\'name\':\'Test\',\'attributes\':[{\'attributeName\':\'One\',\'attributeName\':\'Two\',\'attributeName\':\'Three\'}];
AttributeContainer AttributeContainer;
attributeContainer=mapper.readValue(json,新类型引用(){});
System.out.println(attributeContainer.getAttributeMultiMap());
}
我可以知道您使用的是哪个Multimap实现吗?当然,我在构建Multimap时遇到了问题。我可以知道您使用的是哪个版本的guava吗?@初学者:您是否记得在ObjectMapper
实例上注册guava模块?“没有它,杰克逊是不会理解番石榴的。”Beginer sory,我在示例中有一个错误的代码,现在它是好的。构造生成器的正确方法-ImmutableMultimap.builder=ImmutableMultimap.builder()代码>帖子已更新。修复JSON并使其有效如何?看见