[英]How to remove Empty value from json using Gson or VOlley in android
[英]Remove empty object from json array using GSON
{
"ChangeRequests": [
{}
]
}
使用Gson從JSON數組中刪除空模型。 因此,使用Gson在模型列表中創建一個模型,所有值均為null
data class TestRequest(
@SerializedName("ChangeRequests")
val changeRequests: List<ChangeRequest>
)
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.