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}