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/