Android 使用GSON从json数组中删除空对象
使用Gson从JSON数组中删除空模型。 因为它在模型的列表中创建一个模型,所以使用Gson时所有值都为nullAndroid 使用GSON从json数组中删除空对象,android,arrays,json,kotlin,gson,Android,Arrays,Json,Kotlin,Gson,使用Gson从JSON数组中删除空模型。 因为它在模型的列表中创建一个模型,所以使用Gson时所有值都为null { "ChangeRequests": [ {} ] } 简单的代码:它对我有用 val result = Gson().fromJson(jsonString,TestRequest::class.java) 在将JSON转换为模型后,您正在处理这个问题。我想要的是GsonBuilder级别的东西。 val result = Gson().fromJson(js
{
"ChangeRequests": [
{}
]
}
简单的代码:它对我有用
val result = Gson().fromJson(jsonString,TestRequest::class.java)
在将JSON转换为模型后,您正在处理这个问题。我想要的是GsonBuilder级别的东西。
val result = Gson().fromJson(jsonString,TestRequest::class.java)
Type type = new TypeToken<Map<String, Object>>() {}.getType();
Map<String, Object> data = new Gson().fromJson(jsonString, type);
for (Iterator<Map.Entry<String, Object>> it = data.entrySet().iterator();
it.hasNext();) {
Map.Entry<String, Object> entry = it.next();
if (entry.getValue() == null) {
it.remove();
} else if (entry.getValue().getClass().equals(ArrayList.class)) {
if (((ArrayList<?>) entry.getValue()).size() == 0) {
it.remove();
}
}
}
String json = new GsonBuilder().setPrettyPrinting().create().toJson(data);
System.out.println(json);