用 Jackson 映射一个可以有不同类型的 JSON 字段?
我从 Web 服务获取 JSON,并且无法影响 JSON 格式。下面的 JSON 代码只是一个示例来说明问题。字段 cars
可以是包含 Car
对象的对象,也可以是空字符串。如果我可以更改 Web 服务,我会将空字符串更改为空对象,例如 "cars" : {}
而不是 "cars" : ""
。
当尝试将 JSON 映射到此 Java 对象时:
public class Person {
public int id;
public String name;
public Map<String, Car> cars;
}
这有效:
{
"id" : "1234",
"name" : "John Doe",
"cars" : {
"Tesla Model S" : {
"color" : "silver",
"buying_date" : "2012-06-01"
},
"Toyota Yaris" : {
"color" : "blue",
"buying_date" : "2005-01-01"
}
}
}
这失败了:
{
"id" : "1",
"name" : "The Dude",
"cars" : ""
}
在 Jackson 中处理这种情况的最佳方法是什么?如果有空字符串,我想为 cars
字段获取 null
。我尝试使用 ACCEPT_EMPTY_STRING_AS_NULL_OBJECT
,但没有帮助。
I get JSON from a web service and can not influence the JSON format. The JSON code below is just an example to illustrate the problem. The field cars
can either be an object containing Car
objects or it can be an empty string. If I could change the web service, I'd change the empty String to be an empty object like "cars" : {}
instead of "cars" : ""
.
When trying to map JSON to this Java object:
public class Person {
public int id;
public String name;
public Map<String, Car> cars;
}
This works:
{
"id" : "1234",
"name" : "John Doe",
"cars" : {
"Tesla Model S" : {
"color" : "silver",
"buying_date" : "2012-06-01"
},
"Toyota Yaris" : {
"color" : "blue",
"buying_date" : "2005-01-01"
}
}
}
And this fails:
{
"id" : "1",
"name" : "The Dude",
"cars" : ""
}
What would be the best way to handle this case in Jackson? If there's the empty string, I'd like to get null
for the field cars
. I tried using ACCEPT_EMPTY_STRING_AS_NULL_OBJECT
, but it didn't help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“cars”元素值不是列表(也称为数组)。它是一个JSON对象,也可以认为是一个map类型的集合,但它不是一个列表。
因此,换句话来说,目标是将有时是对象、有时是空字符串的 JSON 反序列化为 Java
Map
。为了解决这个问题,我很惊讶
ACCEPT_EMPTY_STRING_AS_NULL_OBJECT
没用。我建议在 http://jira.codehaus.org/browse/JACKSON 记录问题。您可以实现自定义反序列化。以下是一个示例解决方案。如果目标数据结构有其他
Map
引用,则需要相应更改此解决方案。The "cars" element value is not a list (aka array). It's a JSON object, which can also be considered a map-type collection, but it is not a list.
So, to rephrase the issue, the goal is to deserialize JSON that is sometimes an object and sometimes an empty string into a Java
Map
.To solve this, I'm surprised
ACCEPT_EMPTY_STRING_AS_NULL_OBJECT
didn't work. I recommend logging an issue at http://jira.codehaus.org/browse/JACKSON.You could implement custom deserialization. Following is an example solution. If the target data structure has other
Map
references, then this solution would need to be accordingly changed.