如何在java中使用jackson打開和序列化java map ?

[英]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? I'm using java jackson library for this.

如何忽略上面json中的dataMap層?我用的是java jackson庫。

Code i tried is

我試着代碼

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

4 个解决方案

#1


10  

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. This annotation is available in Jackson 1.9+.

另一種方法是使用@ jsonunwrap注釋。該注釋在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

使用此注釋的缺點是無法使用您的另一個問題的答案中所述的映射

#2


2  

You are getting

你得到了

{ "dataMap": ... } 

because Foo has a field called dataMap within it. Here the name of the field cannot be ignored because it will create conflicts if Foo had multiple maps, something like

因為Foo中有一個名為dataMap的字段。在這里,不能忽略字段的名稱,因為如果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());

#3


1  

I found answer - using @JsonAnyGetter annotation with getter for map. To avoid conflicts during deserialization should be used @JsonAnySetter annotation with setter for this map.

我找到了答案——使用@JsonAnyGetter注釋和getter進行映射。為了避免在反序列化期間發生沖突,應該使用@JsonAnySetter注釋來設置此映射。

#4


0  

In map there is two Objects String and Data . 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.

在map中有兩個對象字符串和數據。JSON將java對象轉換為JSON鍵值對,但是如果你已經有一個包含鍵值對的地圖,地圖轉換成JSON文檔,然后映射鍵也轉換成JSON鍵值對和映射值也有對象轉換成鍵值對。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/08/04/72f5567f0870c27f77a8793a7464697.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com