欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

詳解Java去除json數(shù)據(jù)中的null空值問題

1、描述

在城東等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站建設(shè)、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作定制網(wǎng)站設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計(jì),營(yíng)銷型網(wǎng)站,成都外貿(mào)網(wǎng)站建設(shè)公司,城東網(wǎng)站建設(shè)費(fèi)用合理。

@JsonInclude(JsonInclude.Include.NON_NULL)標(biāo)記是jackson包提供的json序列化方法,已經(jīng)集成于Springboot2.0中,此方法的配置意在可以對(duì)實(shí)體json序列化的時(shí)候進(jìn)行對(duì)應(yīng)的數(shù)值處理。

2、使用

用注解的方式放在標(biāo)記類或者屬性

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

用配置文件的方式全局配置(可能會(huì)影響到其他某些服務(wù)接口),例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;
    }
  }
  
  
  }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文標(biāo)題:詳解Java去除json數(shù)據(jù)中的null空值問題
當(dāng)前URL:http://chinadenli.net/article8/gpggop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)網(wǎng)頁設(shè)計(jì)公司App設(shè)計(jì)網(wǎng)站設(shè)計(jì)搜索引擎優(yōu)化標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)