JSON将HashMap序列化为根元素 [英] JSON Serialize HashMap as root element
本文介绍了JSON将HashMap序列化为根元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类,它基本上是HashMap的包装器。我想用Jackson将这个类序列化为一个没有任何包装元素的JSON对象。
I have a class which is basically a wrapper for a HashMap. I would like to serialize this class with Jackson to an JSON object without any wrapping element.
public class Customers {
@JsonProperty
private Map<String,Customer> customers = new HashMap<>();
...
}
当前序列化如下所示:
{
"Customers":{
"customers":{
"keyX":{...},
"keyY":{...},
"keyZ":{...}
}
但我希望如此:
{
"keyX":{...},
"keyY":{...},
"keyZ":{...}
}
我怎样才能达到它?
推荐答案
为什么不试试这个
jsonString = mapper.writeValueAsString(customerObj.getCustomers());
只需传递Map,而不是Customer对象。
Simply pass the Map, instead of Customer object.
这篇关于JSON将HashMap序列化为根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文