java - Gson 忽略值 = null 的 map 条目

标签 java json gson

Gson gson = new Gson();

Map<String,Object> map = new HashMap<String, Object>();
map.put("a", 1);
map.put("b", null);

System.out.println(gson.toJson(map)); //prints {"a":1}

如何让它包含所有条目?

最佳答案

Gson User Guide - Null Object Support :

The default behaviour that is implemented in Gson is that null object fields are ignored. This allows for a more compact output format; however, the client must define a default value for these fields as the JSON format is converted back into its Java form.

Here's how you would configure a Gson instance to output null:

Gson gson = new GsonBuilder().serializeNulls().create();

关于java - Gson 忽略值 = null 的 map 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3923759/

相关文章:

c# - Json反序列化失败

php - MYSQL限制JOIN行数

android - 外部图书馆规模

java - 当 "parameter"名称改变时,读取Json以Gson返回

java - 用于 Log4j 2 的 Log4jNestedDiagnosticContextFilter

java - 删除双向递归关系的最简单方法?

json - 配置JsonProcessingExceptionMapper登录Dropwizard

java - Gson,创建一个简单的 JsonObjects 的 JsonArray

java - Storm升级到1.2.3时storm列表出错

java - 声明来自套接字的输入/输出流以防止程序继续