java清空json_详解Java去除json数据中的null空值问题|chu

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; } } }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值