首页 文章

忽略来自Rest API Response Java的Null值字段

提问于
浏览
1

在我的项目中,我将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();

从Web项目推进休息客户端:

{
  "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或当我想将其打印到控制台时,它还会打印带有null或空值的fiels:

{
  "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字符串与休息客户端中的响应相同,即我不想打印null或空值字段我只想忽略它们 .

2 回答

相关问题