详解Java去除json数据中的null空值问题

时间:2021-05-19

1、描述

@JsonInclude(JsonInclude.Include.NON_NULL)标记是jackson包提供的json序列化方法,已经集成于Springboot2.0中,此方法的配置意在可以对实体json序列化的时候进行对应的数值处理。

2、使用

用注解的方式放在标记类或者属性

@JsonInclude(JsonInclude.Include.NON_NULL)public class User implements Serializable { private String username; private String password; @JsonInclude(JsonInclude.Include.NON_NULL) private String token; }

用配置文件的方式全局配置(可能会影响到其他某些服务接口),例SpringBoot的application.yml

spring: jackson: default-property-inclusion: non_null

3、示例

  • 使用前:{"username":"admin", "password":"admin123", "token":null}
  • 使用后:{"username":"admin", "password":"admin123"}

PS:Java递归去除Json中的空值

package com.dunyun.openapi.util.json; import java.util.ArrayList;import java.util.List;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;import com.dunyun.platform.util.StringUtil; public class RemoveNullAttr { public static void main(String[] args){ String s="{\"employees\": [{ \"firstName\":\"\" , \"lastName\":\"Doe\" },{ \"firstName\":\"\" , \"lastName\":\"Smith\" },{ \"firstName\":\"Peter\" , \"lastName\":\"Jones\" }]}"; String a="{ \"firstName\":\"John\" , \"lastName\":\"\" }"; String b="{ \"firstName\":\"\" , \"lastName\":\"Doe\" }"; String test="{\"ka\":\"va\",\"kb\":{\"kbk\":\"kbv\",\"kbkk\":\"\",\"kbkkk\":{\"aaa\":\"bbb\",\"bbb\":\"\"},\"suzu\":[{\"sz\":\"sz\",\"szk\":\"\"}]},\"kc\":\"kcv\"}"; JSONObject js=JSONObject.parseObject(test); RemoveNullAttr r=new RemoveNullAttr(); Object o=r.traverseJson(js); System.out.println(o); } private Object traverseJson(Object json) { // check null if (json == null) { return null; } try { if (json instanceof JSONObject) {// if json is a Map JSONObject jsonObj = (JSONObject)json; List keyList=new ArrayList(); for(String k:jsonObj.keySet()){ String value=jsonObj.get(k).toString(); if(StringUtil.isEmpty(value)){ keyList.add(k); //jsonObj.remove(k); }else{ if(isJsonObj(value)){ jsonObj.put(k, traverseJson(JSONObject.parseObject(value))); }else{if(isJsonArr(value)){ //value=; jsonObj.put(k, traverseJson(JSONArray.parseArray(value))) ; } } } } for(String k:keyList){ jsonObj.remove(k); } return jsonObj; } else if (json instanceof JSONArray) {// if json is an Array JSONArray jsonArr = (JSONArray)json; int len = jsonArr.size(); for (int i = 0; i < len; ++i) { // TODO: do something here jsonArr.set(i, traverseJson(jsonArr.get(i))); }// return retArr;// // } else {// if json is just a raw element// // // TODO: do something here// return json; return jsonArr;// } } catch (Exception e) { e.printStackTrace();// deal Exception or throw it } return null; } public boolean isJsonObj(Object o){ try{ JSONObject js=JSONObject.parseObject(o.toString()); return true; }catch(Exception e){ return false; } } public boolean isJsonArr(Object o){ try{ JSONArray js=JSONArray.parseArray(o.toString()); return true; }catch(Exception e){ return false; } } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章