{
"ChangeRequests": [
{}
]
}
使用Gson从JSON数组中删除空模型。 因此,使用Gson
在模型列表中创建一个模型,所有值均为空data class TestRequest(
@SerializedName("ChangeRequests")
val changeRequests: List<ChangeRequest>
)
val result = Gson().fromJson(jsonString,TestRequest::class.java)
答案 0 :(得分:0)
简单的代码:对我有用!
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);