首页 > 代码库 > fastjson null 值处理
fastjson null 值处理
偶然用到fastjson转换json 在前台用js解析竟然某些字段没有,曾经用过gson。联想到是不是相似gson默认将null值不显示了,找了下资料果真如此
直接上代码吧
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
public class Test2 {
/**
* @param args
*/
public static void main(String[] args) {
/*
* 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("xyw",1);
jsonMap.put("123","");
jsonMap.put("xuyw",null);
jsonMap.put("xywa","css");
String str = JSONObject.toJSONString(jsonMap);
System.out.println(str);
String str2 = JSONObject.toJSONString(jsonMap,SerializerFeature.WriteMapNullValue);
System.out.println(str2);
}
}
输出结果
{"123":"","xyw":1,"xywa":"css"}
{"123":"","xuyw":null,"xyw":1,"xywa":"css"}
fastjson null 值处理
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。