簡體   English   中英

從JSON Response(Spring REST)中刪除具有空對象的空對象和空對象

[英]Removing null and empty objects from the JSON Response(Spring REST) with empty object inside empty object

我想從JSON響應中刪除Null和Empty值。

以下是Json String:

{"implDisplayablePricePlan": [
          {
            "productSpecPricing": {
              "childPricingSchema": {}
            },
            "assignedPricePlanID": "abc",
            "name": "GOLD",
            "action": "Something",
            "status": "Something",
            "selected": true,
            "crossProductDiscount": false,

            "displayInformation": {
              "visible": true,
              "enabled": false
            }
          }]

}

在這種情況下:productSpecPricing和childPricingSchema

什么是刪除對任何JSON字符串通用的空對象的最佳方法。

使用Jackson,您可以輕松設置首選項以序列化對象

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
objectMapper.setSerializationInclusion(Include.NON_NULL);
objectMapper.setSerializationInclusion(Include.NON_EMPTY);
ObjectWriter writer = objectMapper.writer();
System.out.println(writer.writeValueAsString(YOUR_OBJECT));

我懂了:

{
  "implDisplayablePricePlan" : [ {
    "productSpecPricing" : { },
    "assignedPricePlanID" : "abc",
    "name" : "GOLD",
    "action" : "Something",
    "status" : "Something",
    "selected" : true,
    "crossProductDiscount" : false,
    "displayInformation" : {
      "visible" : true,
      "enalble" : false
    }
  } ]
}

由於productSpecPricing不顯示為null(或空數組/集合),要解決該問題,您必須添加一個過濾器:

@JsonFilter("myFilter")注釋包含productSpecPricing屬性的類

FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", filter);
ObjectWriter writer = objectMapper.writer(filters);

過濾:

PropertyFilter filter = new SimpleBeanPropertyFilter() {

    @Override
    public void serializeAsField(Object pojo, JsonGenerator jgen,
            SerializerProvider provider, PropertyWriter writer)
                    throws Exception {
        if (include(writer)) {
            System.out.println(writer.getName());
            if (!writer.getName().equals("productSpecPricing")) {
                writer.serializeAsField(pojo, jgen, provider);
                return;
            } else {
                ProductSpecPricing productSpecPricing = ((YOU_OBJECT) pojo).getProductSpecPricing();
                if (productSpecPricing != null && productSpecPricing.getChildPricingSchema() != null && !productSpecPricing.getChildPricingSchema().isEmpty()) {
                    writer.serializeAsField(pojo, jgen, provider);
                }
            }
        } else if (!jgen.canOmitFields()) {
            writer.serializeAsOmittedField(pojo, jgen, provider);
        }
    }

    @Override
    protected boolean include(PropertyWriter writer) {
        return true;
    }

    @Override
    protected boolean include(BeanPropertyWriter writer) {
        return true;
    }
};

應用過濾器后,結果為:

{
  "implDisplayablePricePlan" : [ {
    "assignedPricePlanID" : "abc",
    "name" : "GOLD",
    "action" : "Something",
    "status" : "Something",
    "selected" : true,
    "crossProductDiscount" : false,
    "displayInformation" : {
      "visible" : true,
      "enalble" : false
    }
  } ]
}

這取決於您如何獲取/創建JSON文件。

如果要創建它,則在創建過程中,如果值不存在,為null,void或您不希望的任何其他值,請不要插入它。

但是,如果您從其他人那里接收到此JSON,則在解析過程中,再次獲取鍵/對值,如果您不想要它,請不要將其添加到對象中。

如果您的程序沒有詳細信息,我們將無濟於事

編輯1:

PHP中的這段代碼(改編自我使用的代碼):

 $input = file_get_contents($name, "r"); if($input != FALSE) { for($i=0;$i<strlen($input);$i++){ if($input[$i]=='{'){ $begin= $i; } else if($input[$i]=='}'){ $buffer .= substr($input,$begin,$i-$begin+1) .","; if (strpos($buffer,'":null') !== false) { // Drop the current line } else { // Line is correct, keep on writting } if($counter>=100 ){ $counter= 0; save($buffer); $buffer = ""; } } else if($input[$i]==']'){ save($buffer); $buffer = ""; $i = strlen($input); } } } 

在其中,我們將首先成功獲取JSON文件。 然后,打開文件,讀取JSON結構的每個“部分”(或對象),然后做我們必須做的(在我的情況下,我將修復輸入,但是我想您可以刪除它。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM