杰克逊条件映射

发布于 2025-01-17 21:34:22 字数 1036 浏览 1 评论 0原文

因此,我正在收到一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虐人心 2025-01-24 21:34:22

使用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);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文