Json

JSONObject

JSONObject.toJSONString(productInventory)

将实体对象转换成Json字符串时默认忽略值为null的属性。{"name":"sean","age":"12"}

JSONObject jsonObject = new JSONObject();
jsonObject.put("id", null);
jsonObject.put("class", "12");
jsonObject.put("name", "");
// 使用默认序列化方式打印{"name":"","class":"12"}
System.out.println(jsonObject.toJSONString());
// 使用指定序列化方式  {"id":null,"name":"","class":"12"}
System.out.println(jsonObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue));

posted on 2023-02-08 13:00  焕然一鑫  阅读(26)  评论(0编辑  收藏  举报

导航