JSONObject自动隐藏为null的属性的解决办法

JSON字符串转JSONObject自动隐藏为null的属性

JSON中put的value=null,key=value会被隐藏掉

package hskw.chd;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**  
 * @author hskw  
 * @date 2019年4月4日  
 */
public class JsonTest {

	public static void main(String[] args) {
		String json = "{\"msg\":\"success\",\"code\":0,\"data\":"
				+ "{\"total\":1,\"size\":10,\"pages\":1,\"current\":1,"
				+ "\"records\":[{\"id\":\"88db551925ea4822aeab2ddcd8c66dbe\","
				+ "\"userId\":\"userId\",\"userName\":\"咨询人名称\","
				+ "\"transId\":\"事项id\",\"reuserId\":null,\"consultCode\":"
				+ "\"BH1554275529568\",\"telPhone\":\"电话\",\"title\":\"标题\","
				+ "\"content\":\"咨询内容\",\"addDate\":null,\"reDate\":null,"
				+ "\"reContent\":null,\"reType\":null,\"createBy\":null,"
				+ "\"createDate\":null,\"updateBy\":null,\"updateDate\":null,"
				+ "\"remarks\":\"备注\",\"delFlag\":\"0\"}]}}";
		//将JSON字符串转换为JSONObject对象
		JSONObject object = (JSONObject)JSONObject.parse(json);
		//打印格式化后的JSON对象
		System.out.println(toJson(object));

	}

    /**
     * 对象——格式化后的Json
     * @param object
     * @return
     */
    public static String toJson(Object object){
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting(); //生成格式化后的json
        Gson gson = gsonBuilder.create();
        return gson.toJson(object);
    }
}

为null的属性已被隐藏(在其中,只是隐藏了),运行结果如下 :

JSONObject自动隐藏为null的属性的解决办法_第1张图片

Fastjson的SerializerFeature序列化属性

QuoteFieldNames———-输出key时是否使用双引号,默认为true 
WriteMapNullValue——–是否输出值为null的字段,默认为false 
WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null 
WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null 
WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null 
WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null

 输出被隐藏的属性:

JSONObject自动隐藏为null的属性的解决办法_第2张图片

JSONObject自动隐藏为null的属性的解决办法_第3张图片

有时候我们不需要打印出来,而是让被隐藏的字段显示,前端要用,那就可以在转换前替换一下。

package hskw.chd;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**  
 * @author hskw  
 * @date 2019年4月4日  
 */
public class JsonTest {

	public static void main(String[] args) {
		String json = "{\"msg\":\"success\",\"code\":0,\"data\":"
				+ "{\"total\":1,\"size\":10,\"pages\":1,\"current\":1,"
				+ "\"records\":[{\"id\":\"88db551925ea4822aeab2ddcd8c66dbe\","
				+ "\"userId\":\"userId\",\"userName\":\"咨询人名称\","
				+ "\"transId\":\"事项id\",\"reuserId\":null,\"consultCode\":"
				+ "\"BH1554275529568\",\"telPhone\":\"电话\",\"title\":\"标题\","
				+ "\"content\":\"咨询内容\",\"addDate\":null,\"reDate\":null,"
				+ "\"reContent\":null,\"reType\":null,\"createBy\":null,"
				+ "\"createDate\":null,\"updateBy\":null,\"updateDate\":null,"
				+ "\"remarks\":\"备注\",\"delFlag\":\"0\"}]}}";
		//将JSON字符串中为null替换成"",然后在转换为JSONObject对象
		JSONObject object = (JSONObject)JSONObject.parse(json.replaceAll("null", "\"\""));
		
//		String jsonString = JSONObject.toJSONString(object,SerializerFeature.WriteMapNullValue);
//		System.out.println(jsonString);
		
		//打印格式化后的JSON对象
		System.out.println(toJson(object));

	}

    /**
     * 对象——格式化后的Json
     * @param object
     * @return
     */
    public static String toJson(Object object){
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting(); //生成格式化后的json
        Gson gson = gsonBuilder.create();
        return gson.toJson(object);
    }
}

运行效果:

{
  "msg": "success",
  "code": 0,
  "data": {
    "total": 1,
    "current": 1,
    "pages": 1,
    "size": 10,
    "records": [
      {
        "updateDate": "",
        "transId": "事项id",
        "consultCode": "BH1554275529568",
        "userName": "咨询人名称",
        "title": "标题",
        "delFlag": "0",
        "userId": "userId",
        "addDate": "",
        "content": "咨询内容",
        "telPhone": "电话",
        "reType": "",
        "createBy": "",
        "updateBy": "",
        "reDate": "",
        "reContent": "",
        "id": "88db551925ea4822aeab2ddcd8c66dbe",
        "remarks": "备注",
        "reuserId": "",
        "createDate": ""
      }
    ]
  }
}
 

你可能感兴趣的:(JSONObject自动隐藏为null的属性的解决办法)