如何使用jackson在java中解包和序列化java映射?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18041591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to unwrap and serialize java map in java using Hymanson?
提问by Suren Raju
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 Hymanson library for this.
如何忽略上面json中的dataMap层?我为此使用了 java Hymanson 库。
Code i tried is
我试过的代码是
ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(myFOOObject)
采纳答案by Bart
You could create a getter for dataMapand serialize the dataMapinstead 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 Hymanson 1.9+.
另一种方法是使用@JsonUnwrapped
注释。此注释在 Hymanson 1.9+ 中可用。
http://Hymanson.codehaus.org/1.9.9/javadoc/org/codehaus/Hymanson/annotate/JsonUnwrapped.html
http://Hymanson.codehaus.org/1.9.9/javadoc/org/codehaus/Hymanson/annotate/JsonUnwrapped.html
The downside of using this annotation is the inability to use maps as stated in the answer to your other question
回答by sanbhat
You are getting
你得到
{ "dataMap": ... }
because Foo
has a fieldcalled dataMap
within it. Here the name of the field cannot be ignoredbecause it will create conflictsif 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 clearwhile 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());
回答by Harmeet Singh Taara
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.
在地图中有两个对象String
和Data
。JSON 将 java 对象转换为 JSON 键值对,但是如果您已经有一个包含键值对的 Map,并将该映射转换为 JSON 文档,那么映射键也转换为 JSON 键值对并且 Map 值也有一个对象转换成键值对。
回答by T.Loner
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 来获取地图。为避免在反序列化期间发生冲突,应为此映射使用带有 setter 的 @JsonAnySetter 注释。