问题:
当一个字段为null时,json数据不显示字段名称
Map m = New HashMap();
m.put(“a”, null);
输出:new Gson().toJson(m);
预期结果:{a:null}
实际结果:{}
解决方式:使用 GsonBuilder gsonbuilder = new GsonBuilder().serializeNulls();
添加serializeNulls()方法
val gson = GsonBuilder().serializeNulls().setPrettyPrinting().create() val msg: String = try { gson.toJson(gson.fromJson(responseBodyString, Map::class.java)) } catch (e: Exception) { e.printStackTrace() "" } //Logger.d("Response: %s", msg) Log.i("Response: %s",msg)
标签:null,GsonBuilder,msg,serializeNulls,序列化,gson From: https://www.cnblogs.com/changyiqiang/p/16799916.html