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

发布时间:2020-09-21 19:24:09 作者:走马川行雪
来源:脚本之家 阅读:200

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、示例

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

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

推荐阅读:
  1. php 去除多维数组中的空值
  2. MySQL的空值和NULL的区别是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java json null空值

上一篇:JS字符串去除连续或全部重复字符的实例

下一篇:关于Node.js的events.EventEmitter用法介绍

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》