杰克逊序列化从列表中排除对象



以下JSON有一些父子关系(示例数据(

    ">
  • id":1050 是子级,因为它有 "parentId":1051
  • ">
  • id":1051 是父级,因为它有 "parentId":空

像这样我有巨大的数据

      {
        "statusCode": 200,
        "statusMessage": "Success",
        "dataCount": 0,
        "data": null,
        "dataList": [
          {
            "id": 1050,
            "parentId": 1051,
            "questionInfo": {
              "id": 1050,
              "description": "--",
              "question": "--",
              "answersInfo": [
                {
                  "id": 2041,
                  "description": "--",
                  "isCorrect": "--",
                  "name": "--",
                  "subQuestions": []
                },
                {
                  "id": 2040,
                  "description": "--",
                  "isCorrect": "--",
                  "name": "--",
                  "subQuestions": []
                }
              ]
            }
          },
          {
            "id": 1051,
            "parentId": null,
            "questionInfo": {
              "id": 1051,
              "description": "--",
              "question": "--",
              "answersInfo": [
                {
                  "id": 2024,
                  "description": "--",
                  "isCorrect": "--",
                  "name": "--",
                  "subQuestions": []
                },
                {
                  "id": 2023,
                  "description": "--",
                  "isCorrect": "--",
                  "name": "--",
                  "subQuestions": [
                    {
                      "id": 1050,
                      "parentId": 1051,
                      "questionInfo": {
                        "id": 1050,
                        "description": "--",
                        "question": "--",
                        "answersInfo": [
                          {
                            "id": 2041,
                            "description": "--",
                            "isCorrect": "--",
                            "name": "--",
                            "subQuestions": []
                          },
                          {
                            "id": 2040,
                            "description": "--",
                            "isCorrect": "--",
                            "name": "--",
                            "subQuestions": []
                          }
                        ]
                      }
                    }
                  ]
                }
              ]
            }
          },
          {
            "-------": "-------"
          }
        ],
        "dataMap": null
      }

我的要求是需要从parentId具有某些值("parentId":1051(的父位置中删除子记录,因为这些子信息来自父子信息,这些记录不是父记录

      {
        "statusCode": 200,
        "statusMessage": "Success",
        "dataCount": 0,
        "data": null,
        "dataList": [
          {
            "id": 1051,
            "parentId": null,
            "questionInfo": {
              "id": 1051,
              "description": "--",
              "question": "--",
              "answersInfo": [
                {
                  "id": 2024,
                  "description": "--",
                  "isCorrect": "--",
                  "name": "--",
                  "subQuestions": []
                },
                {
                  "id": 2023,
                  "description": "--",
                  "isCorrect": "--",
                  "name": "--",
                  "subQuestions": [
                    {
                      "id": 1050,
                      "parentId": 1051,
                      "questionInfo": {
                        "id": 1050,
                        "description": "--",
                        "question": "--",
                        "answersInfo": [
                          {
                            "id": 2041,
                            "description": "--",
                            "isCorrect": "--",
                            "name": "--",
                            "subQuestions": []
                          },
                          {
                            "id": 2040,
                            "description": "--",
                            "isCorrect": "--",
                            "name": "--",
                            "subQuestions": []
                          }
                        ]
                      }
                    }
                  ]
                }
              ]
            }
          },
          {
            "-------": "-------"
          }
        ],
        "dataMap": null
      }

我正在使用Spring MVC + Hibernate标准API + Jackson库那么,任何人都可以告诉我在序列化时从列表中排除对象(基于某些条件的完整对象(的最佳方法?

正如我们与@Developer讨论的那样,回答这个问题的最佳方法是使用某种工具序列化所有对象,就像从Jackson ObjectMapper一样,一旦构建了 List,迭代它以仅包含您想要保留的元素。

性能方面,这不是最好的事情,但这将确保您不会错过任何想要的物品

相关内容

  • 没有找到相关文章

最新更新