简体   繁体   中英

How to exclude a Null List in a JSON object while Parsing to POJO

I have A JSON Response which is having some Object and Objects have some List of values.

"A": {
  "Object1": {
     "list":[
         null,
         null,
         null
       ]
   }
   "Object2": {
     "list":[
         null,
         null,
         null
       ]
   }
   "Object3": {
     "list":[
         2,
         3,
         8
       ]
   }
 }

I want to exclude the Complete Object "Object1" and "Object2" if the list is having null values.

I tried

@JsonInclude(Include.NON_NULL) 

both at class level and field level but still its not excluding.

You can use @JsonSetter annotation on the field with specification, how nulls should be handled:

@JsonSetter(nulls=Nulls.AS_EMPTY) // null will clear to empty list
public List<String> names = Collections.emptyList();

(you need at Jackson in version at least 2.9)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM