我想从JSON Response中删除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 with childPricingSchema
删除任何JSON字符串通用的空对象的最佳方法是什么。
答案 0 :(得分:2)
使用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(或空数组/集合),所以要解决它,你必须添加一个过滤器:
使用productSpecPricing
@JsonFilter("myFilter")
属性的类
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
}
} ]
}
答案 1 :(得分:0)
这取决于您获取/创建JSON文件的方式。
如果您正在创建它,那么,在创建过程中,如果某个值不存在,null,void或您不想要的任何其他值,请不要插入它。
但是,如果您从其他人那里收到此JSON,那么在解析期间,再次获取键/对值,如果您不想要它,请不要添加到您的对象。
如果没有来自您的程序的详细信息,我们无法提供更多帮助
$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结构的每个“部分”(或对象),然后按我们的意愿执行(在我的情况下,我会修复输入,但我想你可以放弃它。)