杰克逊条件映射
因此,我正在收到一个JSON字符串,并试图将其映射到嵌套几个度的POJO。该对象之一是列表,并包含事件列表(事件类)。如果事件类中的某个字段等于“ x”,我只想映射事件并将其添加到列表中。输入字符串很大,我只抓住少数属性。然后,一旦映射,如果我将其打印成字符串,我会说...
{
"status":"completed",
"_embedded":{
"events":[
{
"type":"started"
},
{
"type":"completed"
},
{
"type":"started"
},
{
"type":"completed"
},
{
"type":"started"
},
{
"type":"completed"
}
]
}
}
我现在想要一种将映射绘制为条件的方法...
{
"status":"completed",
"_embedded":{
"events":[
{
"type":"completed"
},
{
"type":"completed"
},
{
"type":"completed"
}
]
}
}
因此,只有当“类型”“已完成”时,我才能将这些事件对象保存在数组中,丢弃其余的。
So I am receiving a JSON string and trying to map to a POJO that is nested a few degrees. One of the object is a List and contains a list of events (Event class). I want to ONLY map the Event and add it to the list if a certain field in the Event class is equal to "x". The input String is massive and I'm only grabbing a handful of attributes. Then once it is mapped, if I print it out as a String I get let's say...
{
"status":"completed",
"_embedded":{
"events":[
{
"type":"started"
},
{
"type":"completed"
},
{
"type":"started"
},
{
"type":"completed"
},
{
"type":"started"
},
{
"type":"completed"
}
]
}
}
I now want a way to make the mapping conditional so that when I map to a POJO and then print it out as a String I would get...
{
"status":"completed",
"_embedded":{
"events":[
{
"type":"completed"
},
{
"type":"completed"
},
{
"type":"completed"
}
]
}
}
So only if the "type" is "completed" I would keep those event objects in the array, discard the rest.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用Google GSON LIB也支持仿制药和嵌套豆。
示例
data data = new GSON()。FromJSON(JSON,DATA.CLASS);
Use Google Gson lib that supports generics and nested beans also.
example
Data data = new Gson().fromJson(json, Data.class);