简体   繁体   English

如何使用jackson在java中解包和序列化java地图?

[英]How to unwrap and serialize java map in java using jackson?

I have a bean like this 我有这样的豆子

class Foo {
    private Map<String, Data> dataMap;
    private String fooFieldOne;
    private String fooFieldTwo;
}

class Data {
    private fieldOne;
    private fieldTwo;
}

I want to serialize as Json as like this 我想像Json一样序列化

{
    "key1": {
        "fieldOne": "some value",
        "fieldTwo": "some value"
    },
    "key2": {
        "fieldOne": "some other value",
        "fieldTwo": "some other value"
    },
    "fooFieldOne":"valueone", 
    "fooFieldTwo":"valuetwo" 
}

But i am getting result like 但我得到的结果就像

{
    "dataMap": {
        "key1": {
            "fieldOne": "some value",
            "fieldTwo": "some value"
        },
        "key2": {
            "fieldOne": "some other value",
            "fieldTwo": "some other value"
        }
    },
    "fooFieldOne":"valueone", 
    "fooFieldTwo":"valuetwo" 
}

How to ignore dataMap layer in the above json? 如何在上面的json中忽略dataMap图层? I'm using java jackson library for this. 我正在使用java jackson库。

Code i tried is 我试过的代码是

ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(myFOOObject)

You could create a getter for dataMap and serialize the dataMap instead of the entire Foo instance. 您可以为dataMap创建一个getter并序列化dataMap而不是整个Foo实例。

mapper.writeValueAsString(myFOOObject.getDataMap());

Another method is using the @JsonUnwrapped annotation. 另一种方法是使用@JsonUnwrapped注释。 This annotation is available in Jackson 1.9+. 这个注释可以在Jackson 1.9+中找到。

http://jackson.codehaus.org/1.9.9/javadoc/org/codehaus/jackson/annotate/JsonUnwrapped.html http://jackson.codehaus.org/1.9.9/javadoc/org/codehaus/jackson/annotate/JsonUnwrapped.html

The downside of using this annotation is the inability to use maps as stated in the answer to your other question 使用此注释的缺点是无法使用您的其他问题答案中所述的地图

You are getting 你来了

{ "dataMap": ... } 

because Foo has a field called dataMap within it. 因为Foo有一个名为dataMap字段 Here the name of the field cannot be ignored because it will create conflicts if Foo had multiple maps, something like 这里不能忽略该字段的名称,因为如果Foo有多个映射,它会产生冲突 ,例如

class Foo {
private Map<String, Data> dataMap1;
private Map<String, Data> dataMap2;
}

now without having the fieldName : in JSON, the JSON doesn't know how to deserialize when it gets some JSON like 现在没有fieldName:在JSON中,当JSON获得一些类似JSON时,它不知道如何反序列化

{
    //belongs to dataMap1
    "key1": {
        "fieldOne": "some value",
        "fieldTwo": "some value"
    },
    "key2": {
        "fieldOne": "some value",
        "fieldTwo": "some value"
    },
    //belongs to dataMap2
    "key3": {
        "fieldOne": "some value",
        "fieldTwo": "some value"
    },
    "key4": {
        "fieldOne": "some other value",
        "fieldTwo": "some other value"
    }
}

Now having the fieldName makes it very clear while deserializing 现在拥有fieldName使反序列化时非常清楚

{
    "dataMap1" : {
    "key1": {
        "fieldOne": "some value",
        "fieldTwo": "some value"
    },
    "key2": {
        "fieldOne": "some value",
        "fieldTwo": "some value"
    }
    },
    "dataMap2" : {
    "key3": {
        "fieldOne": "some value",
        "fieldTwo": "some value"
    },
    "key4": {
        "fieldOne": "some other value",
        "fieldTwo": "some other value"
    }
  }
}

Having said that 话说回来

How to ignore dataMap layer in the json? 如何忽略json中的dataMap层?

You can just serialize the map 您只需序列化地图即可

ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(myFOOObject.getDataMap());

I found answer - using @JsonAnyGetter annotation with getter for map. 我找到了答案 - 使用@JsonAnyGetter注释和getter for map。 To avoid conflicts during deserialization should be used @JsonAnySetter annotation with setter for this map. 为了避免在反序列化期间发生冲突,应使用@JsonAnySetter注释和此映射的setter。

In map there is two Objects String and Data . 在地图中有两个对象StringData JSON convert the java objects into JSON key value pair , but if you already have a Map that contain key value pair, and convert that map into JSON document , then map key is also convert into JSON key value pair and Map value also have a object that convert into key value pair. JSON将java对象转换为JSON键值对,但如果已经有一个包含键值对的Map,并将该映射转换为JSON文档,那么map键也会转换为JSON键值对,而Map值也有一个对象转换为键值对。

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

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