'Remove empty object from json array using GSON
{
"ChangeRequests": [
{}
]
}
Remove the empty model from JSON array using Gson. Because of it create the one model inside the list in the model all value is null using Gson
data class TestRequest(
@SerializedName("ChangeRequests")
val changeRequests: List<ChangeRequest>
)
val result = Gson().fromJson(jsonString,TestRequest::class.java)
Solution 1:[1]
simple code : it worked for me !
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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alireza Haji gholamreza |
