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

时间:2017-02-07 13:40:03

标签: java json spring jackson fasterxml

以下JSON有一些父子关系(样本数据)

  • “id”:1050是孩子,因为它有“parentId”:1051
  • “id”:1051是父级,因为它具有“parentId”:null

像这样我有大量数据

      {
        "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库 所以,有人可以告诉我在序列化时从列表中排除对象(基于某些条件的完整对象)的最佳方法吗?

1 个答案:

答案 0 :(得分:0)

在我们与@Developer讨论时,回答此问题的最佳方法是使用某个工具序列化所有对象,就像ObjectMapper中的Jackson一样,并且一旦构建了List,迭代它只有你想要保留的元素。

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