JSON.toJSONString(Object object,SerializerFeature...features)详解
package myProject;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
public class FastJsonTest {
private int a;
private BigDecimal b;
private String c;
private List<Student> list;
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public BigDecimal getB() {
return b;
}
public void setB(BigDecimal b) {
this.b = b;
}
public List<Student> getList() {
return list;
}
public void setList(List<Student> list) {
this.list = list;
}
public static void main(String[] args) {
FastJsonTest f=new FastJsonTest();
String json1=JSON.toJSONString(f);
String json2=JSON.toJSONString(f,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullNumberAsZero);
System.out.println("json1"+json1);
System.out.println("json2"+json2);
}
}
输出结果为:
json1{“a”:0}
json2{“a”:0,“b”:0,“c”:"",“list”:[]}
可见,BigDecimal类型,String类型,List类型在不添加对应参数,都不会显示出来。