【发布时间】:2017-11-18 06:26:26
【问题描述】:
最近,我遇到了 Gson 反序列化的问题。这是我的对象:
class Foo{
public List<Donation> donations;
class Donation{
// SerializedName here.
}
}
这是我的 json:
{
"donations": [] // sometimes, this array is empty.
}
// this is none empty one.
{
"donations": [
{
"doner_id": 4,
"name": "abc",
"donate": "103"
}
]
}
当用上面的对象解析这个 JSON 时,我总是得到错误:Expected a com.google.gson.JsonObject but was com.google.gson.JsonArray。如果上面的donations 不为空,则一切正常。我该如何解决?
【问题讨论】:
-
你能用数据发布json捐赠吗
-
@vikaskumar 我添加了。
-
如果捐赠列表的大小或长度>0,只需检查条件,然后应用您的代码,否则什么也不做。
-
检查jsonarray.length() > 0 然后通过里面得到json。
标签: android gson json-deserialization