jsonObject.toJSONString() VS jsonObject.toString()

jar包:com.alibaba.fastjson

jsonObject.toJSONString() 源码:

public String toJSONString() {
      SerializeWriter out = new SerializeWriter();
        try {
            new JSONSerializer(out).write(this);
            return out.toString();
        } finally {
            out.close();
        }
}

jsonObject.toString()  源码:

public String toString() { 
    return toJSONString(); 
}
public static void main(String[] args){
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("a",null);
    jsonObject.put("b","");
    System.out.println(jsonObject.toJSONString());
 }

输出结果:

{"b":""}
{"b":""}

toString方法,自动过滤掉null

 

你可能感兴趣的:(jsonObject.toJSONString() VS jsonObject.toString())