英文:
Serialising a Java Map with Jackson
问题
Sure, here's the translated content:
我正在使用Jackson库进行JSON序列化,
在序列化Java Map时,例如,map<String,String>
包含
{<color, green>,<color, blue>}
我希望将其序列化为
"colormap":[{"key": "color", "value":"green"}, {"key": "color", "value":"blue"}]
但它总是序列化为
"colormap":[{"color":"green"}, {"color":"blue"}]
英文:
I am using Jackson library for serialising JSON,
having Serialising a Java Map, for ex, map<String,String>
has
{<color, green>,<color, blue>}
I want this to be serialised as
"colormap":[{"key": "color":, "value":"green"}, {"key": "color:, "value":"blue"}]
but its always serialising as
"colormap":[{"color":"green"}, {"color:"blue"}]
答案1
得分: 1
你的输出 JSON 是一个数组,而不是一个映射(map)。在没有查看你的代码的情况下,很难确定底层的数据结构是什么,但为了实现你所需求的功能,你可以考虑使用如下所示的类:
class Thing {
private String key;
private String value;
/// 根据需要添加访问器(accessors)
}
然后将你的颜色映射声明为 `List<Thing>`。这样应该可以按照你的期望对数据进行序列化(serialize)。
英文:
Your output JSON is an array, not a map- Without looking at your code it's hard to tell what the underlying data structure is,but to do what you're looking for you might consider a class such as:
class Thing {
private String key ;
private String value ;
/// add accessors as needed
}
and then declare your colormap as List<Thing>
. This should seralize your data per your expectation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论