讓JSONObject 轉換時保留為null的字段


package com.tsvv.test;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class Test0622 {
    
    public static void main(String[] args) {
        String text = "此處省略業務JSON";
        JSONObject json = JSONObject.parseObject(text);
        JSONObject userIdAndCodeJSON = (JSONObject)json.get("userIdAndCodeJSON");
        userIdAndCodeJSON.put("userId", null);
        String result = JSONObject.toJSONString(userIdAndCodeJSON, SerializerFeature.WriteMapNullValue);
        System.out.println(result);
    }

}

 直接轉換時結果為:{"flag":"0","userCode":"user222"};

 用

JSONObject.toJSONString(JSONObject對象 , SerializerFeature.WriteMapNullValue);

轉換時結果為:{"flag":"0","userCode":"user222","userId":null}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM