為什么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"}

對規則的理解:

  1. SerializerFeature.WriteMapNullValue 是否輸出值為null的字段,默認為false
    也就是說有null時會輸出而不是忽略(默認策略是忽略,所以看不到為null的字段)

  2. WriteNullStringAsEmpty—字符類型字段如果為null,輸出為”“,而非null 
    注意是字段字段字段,而不是json.put("key",null),所以用它時,字段為null的可以轉換為空字符串。

  3. 如果讓輸出的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

 


免責聲明!

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



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