如何使用 JSON 消除空值变量

How to eliminate variables with null value using JSON

我收到 json 以下格式的回复

{
    "employees": [
        {
            "emp_id": 101,
            "name": null,
            "company": "Onmobile",
            "test_beans": null
        },
        {
            "emp_id": 101,
            "name": null,
            "company": "Onmobile",
            "test_beans": null
        }
    ],
    "organizations": [
        {
            "name": "Google"
        },
        {
            "name": "Facebook"
        }
    ]
}

name是String类型的属性,test_beans是某个class的引用变量。因此,在获得响应时,我想排除那些具有空值的变量。 我正在使用 spring rest-api。我有一个自定义的 gsonconverter...所以请帮助我了解 gson 构建器需要做什么,以便它可以消除那些空变量。

只要您不为 GsonBuilder 设置 serializeNulls(),默认情况下会排除 Null 值。