在我的项目中,当我将 Rest Response 发送到Advance Rest Client时,它只显示具有某些值的字段和忽略(不显示)具有 NULL 值或空值的字段。
部分代码:
Gson gson=new Gson();
// firstResponse is Object which contains the values
String jsonString = gson.toJson(firstResponse);
test.saveJson(jsonString); //OR System.out.println(jsonString);
return Response.ok(firstResponse).build(); // Response to Rest Client
响应样本至return Response.ok(firstResponse).build();
来自网络项目的高级休息客户端:
{
"Name": "smith",
"Properties": {
"propertyList": [
{
"ID": "072",
"Number": "415151",
"Address": "Somewhere"
},
{
"ID": "151",
"Number": "a800cc79-99d1-42f1-aeb4-808087b12c9b",
"Address": "ninink"
},
{
"ID": "269",
},
],
},
}
现在,当我将其保存为 DB 中的 Json String 或当我想将其打印到控制台时,它还会打印具有空值或空值的字段:
{
"Name": "smith",
"Properties": {
"propertyList": [
{
"ID": "072",
"Number": "415151",
"Address": "Somewhere"
},
{
"ID": "151",
"Number": "a800cc79-99d1-42f1-aeb4-808087b12c9b",
"Address": "ninink"
},
{
"ID": "269",
"Number": "",
"Address": ""
},
],
},
"resultList" :[]
}
我如何打印或保存这个 JSON 字符串与 rest 客户端中的响应相同,即我不想打印 null 或空值字段,我只想忽略它们。