为什么fastjson字段为null时不输出空字符串?
为什么fastjson字段为null时不输出空字符串?
Map < String , Object > jsonMap = new HashMap< String , Object>();
jsonMap.put("a",1);
jsonMap.put("b","");
jsonMap.put("c",null);
jsonMap.put("d","wuzhuti.cn");
String str = JSONObject.toJSONString(jsonMap);
System.out.println(str);
//输出结果:{"a":1,"b":"",d:"wuzhuti.cn"}
从输出结果可以看出,null对应的key已经被过滤掉;这明显不是我们想要的结果,这时我们就需要用到fastjson的SerializerFeature序列化属性
也就是这个方法:JSONObject.toJSONString(Object object, SerializerFeature... features)
SerializerFeature有用的一些枚举值
QuoteFieldNames———-输出key时是否使用双引号,默认为true
WriteMapNullValue——–是否输出值为null的字段,默认为false
WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null
WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null
WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null
WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null
在加上
Map < String , Object > jsonMap = new HashMap< String , Object>();
jsonMap.put("a",1);
jsonMap.put("b","");
jsonMap.put("c",null);
jsonMap.put("d","wuzhuti.cn");
String str = JSONObject.toJSONString(jsonMap,SerializerFeature.WriteMapNullValue);
System.out.println(str);
//输出结果:{"a":1,"b":"","c":null,"d":"wuzhuti.cn"}
如果把WriteNullStringAsEmpty也加进去,为啥不起作用?!?!?
String str = JSONObject.toJSONString(jsonMap,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty);
System.out.println(str);
//输出结果:{"a":1,"b":"","c":null,"d":"wuzhuti.cn"}
对规则的理解:
SerializerFeature.WriteMapNullValue 是否输出值为null的字段,默认为false
也就是说有null时会输出而不是忽略(默认策略是忽略,所以看不到为null的字段)WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null
注意是字段是字段是字段,而不是json.put("key",null),所以用它时,字段为null的可以转换为空字符串。如果让输出的json中所有为null的字符串都变成空字符串,最简单的做法就是加一个值过滤器,这样就避免了有的字段为null,有的字段为空字符的现象。
贴点示例代码
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
public class Demo1 {
public class Student {
private String name;
private int age;
private boolean isMale;
private Student gf;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isMale() {
return isMale;
}
public void setMale(boolean isMale) {
this.isMale = isMale;
}
public Student getGf() {
return gf;
}
public void setGf(Student gf) {
this.gf = gf;
}
}
private static ValueFilter filter = new ValueFilter() {
@Override
public Object process(Object obj, String s, Object v) {
if (v == null)
return "";
return v;
}
};
public static void main(String[] args) {
new Demo1().foo();
new Demo1().bar();
}
private void foo() {
System.out.println("foo()---------------------------");
JSONObject j1 = new JSONObject();
j1.put("name", "zhangsan");
j1.put("age", 13);
j1.put("isMale", true);
j1.put("gf", null);
Map<String, Object> fav = new HashMap<String, Object>();
Set<String> books = new HashSet<String>();
books.add("三国");
books.add("史记");
fav.put("history", books);
String[] arts = new String[] {};
fav.put("arts", arts);
String[] musics = new String[] { "北京欢迎你", "画心" };
fav.put("musics", musics);
List<String> sports = new ArrayList<String>();
fav.put("sports", sports);
j1.put("fav", fav);
List<Student> classmates = new ArrayList<Student>();
classmates.add(new Student());
Student lisi = new Student();
lisi.setMale(false);
lisi.setAge(11);
classmates.add(lisi);
Student zhangsan = new Student();
zhangsan.setAge(13);
zhangsan.setName("张三");
zhangsan.setMale(true);
zhangsan.setGf(lisi);
classmates.add(zhangsan);
j1.put("classmates", classmates);
String str = null;
j1.put("str", str);
System.out.println(j1.toString());
System.out
.println(JSON.toJSONString(j1, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty));
System.out.println(
JSON.toJSONString(j1, filter, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty));
System.out.println(JSON.toJSONString(j1, SerializerFeature.WriteNullStringAsEmpty));
System.out.println(JSON.toJSONString(j1, filter, SerializerFeature.WriteNullStringAsEmpty));
Map<String, JSONObject> m = new HashMap<String, JSONObject>();
m.put("key", j1);
System.out.println(
JSON.toJSONString(m, SerializerFeature.WriteNonStringKeyAsString, SerializerFeature.WriteNullStringAsEmpty));
System.out.println(JSON.toJSONString(m, filter, SerializerFeature.WriteNonStringKeyAsString,
SerializerFeature.WriteNullStringAsEmpty));
}
private void bar() {
System.out.println("bar()---------------------------");
Student zhangsan = new Student();
zhangsan.setAge(13);
zhangsan.setName("张三");
zhangsan.setMale(true);
Student lisi = new Student();
// lisi.setName("lisi");
lisi.setMale(false);
lisi.setAge(11);
zhangsan.setGf(lisi);
System.out.println(
JSON.toJSONString(zhangsan, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty));
System.out.println(JSON.toJSONString(zhangsan, SerializerFeature.WriteMapNullValue));
System.out.println(JSON.toJSONString(zhangsan, SerializerFeature.WriteNullStringAsEmpty));
System.out.println(JSON.toJSONString(zhangsan));
System.out.println(JSON.toJSONString(zhangsan, filter));
System.out.println(JSON.toJSONString(zhangsan, filter, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty));
}
}
输出结果:
foo()---------------------------
{"isMale":true,"name":"zhangsan","classmates":[{"age":0,"male":false},{"age":11,"male":false},{"age":13,"gf":{"$ref":"$.classmates[1]"},"male":true,"name":"张三"}],"fav":{"sports":[],"musics":["北京欢迎你","画心"],"history":["史记","三国"],"arts":[]},"age":13}
{"str":null,"isMale":true,"name":"zhangsan","classmates":[{"age":0,"gf":null,"male":false,"name":""},{"age":11,"gf":null,"male":false,"name":""},{"age":13,"gf":{"$ref":"$.classmates[1]"},"male":true,"name":"张三"}],"fav":{"sports":[],"musics":["北京欢迎你","画心"],"history":["史记","三国"],"arts":[]},"age":13,"gf":null}
{"str":"","isMale":true,"name":"zhangsan","classmates":[{"age":0,"gf":"","male":false,"name":""},{"age":11,"gf":"","male":false,"name":""},{"age":13,"gf":{"$ref":"$.classmates[1]"},"male":true,"name":"张三"}],"fav":{"sports":[],"musics":["北京欢迎你","画心"],"history":["史记","三国"],"arts":[]},"age":13,"gf":""}
{"isMale":true,"name":"zhangsan","classmates":[{"age":0,"male":false},{"age":11,"male":false},{"age":13,"gf":{"$ref":"$.classmates[1]"},"male":true,"name":"张三"}],"fav":{"sports":[],"musics":["北京欢迎你","画心"],"history":["史记","三国"],"arts":[]},"age":13}
{"str":"","isMale":true,"name":"zhangsan","classmates":[{"age":0,"gf":"","male":false,"name":""},{"age":11,"gf":"","male":false,"name":""},{"age":13,"gf":{"$ref":"$.classmates[1]"},"male":true,"name":"张三"}],"fav":{"sports":[],"musics":["北京欢迎你","画心"],"history":["史记","三国"],"arts":[]},"age":13,"gf":""}
{"key":{"isMale":true,"name":"zhangsan","classmates":[{"age":0,"male":false},{"age":11,"male":false},{"age":13,"gf":{"$ref":"$.key.classmates[1]"},"male":true,"name":"张三"}],"fav":{"sports":[],"musics":["北京欢迎你","画心"],"history":["史记","三国"],"arts":[]},"age":13}}
{"key":{"str":"","isMale":true,"name":"zhangsan","classmates":[{"age":0,"gf":"","male":false,"name":""},{"age":11,"gf":"","male":false,"name":""},{"age":13,"gf":{"$ref":"$.key.classmates[1]"},"male":true,"name":"张三"}],"fav":{"sports":[],"musics":["北京欢迎你","画心"],"history":["史记","三国"],"arts":[]},"age":13,"gf":""}}
bar()---------------------------
{"age":13,"gf":{"age":11,"gf":null,"male":false,"name":""},"male":true,"name":"张三"}
{"age":13,"gf":{"age":11,"gf":null,"male":false,"name":null},"male":true,"name":"张三"}
{"age":13,"gf":{"age":11,"male":false},"male":true,"name":"张三"}
{"age":13,"gf":{"age":11,"male":false},"male":true,"name":"张三"}
{"age":13,"gf":{"age":11,"gf":"","male":false,"name":""},"male":true,"name":"张三"}
{"age":13,"gf":{"age":11,"gf":"","male":false,"name":""},"male":true,"name":"张三"}
转自:https://blog.csdn.net/u012534163/article/details/88741884
为什么fastjson字段为null时不输出空字符串?的更多相关文章
- 如果不空null并且不是空字符串才去修改这个值,但这样写只能针对字符串(String)类型,如果是Integer类型的话就会有问题了。 int i = 0; i!=''。 mybatis中会返回tr
mybatis 参数为Integer型数据并赋值0时,有这样一个问题: mybatis.xml中有if判断条件判断参数不为空时,赋值为0的Integer参数被mybatis判断为空,因此不执行< ...
- Javascript-关于null、undefined、空字符串的区分
一.分别判断 var a=null; //var a=undefined; //var a=''; //var a='DD'; if(!a&&typeof a == 'object') ...
- sql 将Null 值转化成空字符串
当Null + 任何字符串时,都等于Null. 因些用函数IsNull(字段名,''),如果字段名中的值是Null时,那么这个字段名的值是''. 例如::select code + IsNull('- ...
- Javascript 中的false、0、null、undefined和空字符串对象
在Javascript中,我们经常会接触到题目中提到的这5个比较特别的对象——false.0.空字符串.null和undefined.这几个对象很容易用错,因此在使用时必须得小心. 类型检测 我们下来 ...
- spark sql cache时发现的空字符串问题
博客园首发,转帖请注明地址:https://www.cnblogs.com/tzxxh/p/10267202.html 图一 图1未做cache,直接过滤expression列的 null 和空字符串 ...
- Javascript中那些你不知道的事之-- false、0、null、undefined和空字符串
话不多说直接进入主题:(如果有写的不对的地方欢迎指正) 我们先来看看他们的类型分别是什么: typeof类型检测结果 结论:false是布尔类型对象,0是数字类型对象,null是object对象,un ...
- dataframe去除null、NaN和空字符串
去除null.NaN 去除 dataframe 中的 null . NaN 有方法 drop ,用 dataframe.na 找出带有 null. NaN 的行,用 drop 删除行: import ...
- json字段为null时输出空字符串
Map < String , Object > jsonMap = new HashMap< String , Object>(); jsonMap.put(); jsonMa ...
- MongoDB批量操作时字段为null时没有入库
今天在Java后端批量插入数据至MongoDB后,在MongoDB数据库中发现某个字段没有成功入库,一查看代码,在List的元素对象中是有这个字段的,不知为啥就没有入库了. (1)调试 遇到此情况,赶 ...
随机推荐
- sqlserver中产生随机字符,随机数
SELECT REPLACE(NEWID(),'-','') select replicate(char(cast(rand()*1000 as int)%26+97) +char(cast(rand ...
- 51Nod 1701 最后的机会
1701 最后的机会 给定一字符串S,S非空,由小写字母组成,设v为S中元音字母的个数,c为辅音字母的个数."a", "e", "i", & ...
- [HNOI2009]最小圈 分数规划 spfa判负环
[HNOI2009]最小圈 分数规划 spfa判负环 题面 思路难,代码简单. 题目求圈上最小平均值,问题可看为一个0/1规划问题,每个边有\(a[i],b[i]\)两个属性,\(a[i]=w(u,v ...
- UVALive 3716 DNA Regions ——(扫描法)
乍一看这个问题似乎是很复杂,但其实很好解决. 先处理出每个点到原点的距离和到x正半轴的角度(从x正半轴逆时针旋转的角度).然后以后者进行排序. 枚举每一个点到圆心的距离,作为半径,并找出其他到圆心距离 ...
- HDU 4374 One hundred layer(单调队列DP)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=116242#problem/E 题意:差不多就是男人勇下百层的游戏.从第一层到最 ...
- 一个XP SP3调用0地址蓝屏BUG
0x00 蓝屏的堆栈 在XP SP3上跑POC之后,一段时间之后会出现蓝屏,蓝屏的堆栈如下,可以看出是ACKData里面CALL了一个0指针导致的蓝屏 0x01 蓝屏原因 1 ETW(Event Tr ...
- 通过Vagrant搭建PHP环境(一) Vagrant box添加配置
系统Windows10 Vagrant 1.8.1 VirtualBox 5.0.20 vagrant box下载地址:http://cloud.centos.org/centos/7/vagrant ...
- MongoDB(mongodb-win32-x86_64-enterprise-windows-64-4.2.1-signed.msi)下载,启动和插入数据,查询
下载链接:https://pan.baidu.com/s/19lM5Q-_BaDbjaO1Pj0SbYg&shfl=sharepset 安装一路Next就行,安装完毕后,进入目录C:\Prog ...
- MongoDB作为windows服务来安装-2
首先区官网下载对应版本的安装文件,我本地的环境是win7 bit64 我下载的版本是:mongodb-win32-x86_64-2.4.6 ok, 文件下载后,开始安装,这里要说一下,如果直接启动Mo ...
- cs6.8-oracle挂载ceph
https://ceph-users.ceph.narkive.com/EgcYJhbG/hammer-0-94-1-still-getting-feature-set-mismatch-for-ce ...