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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-04-03 我要评论

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;
    }
  }
  
  
  }
您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们