我正在研究使用谷歌的GSON为我的Android项目,将从我的Web服务器请求JSON。的JSON返回将是要么...GSON:知道要转换为什么类型的对象?
1)已知的类型(例如中成功的响应:类“用户”):
{
"id":1,
"username":"bob",
"created_at":"2011-01-31 22:46:01",
"PhoneNumbers":[
{
"type":"home",
"number":"+1-234-567-8910"
},
{
"type":"mobile",
"number":"+1-098-765-4321"
}
]
}
2.)不成功响应,这将始终把上下面是相同的基本结构。
{
"error":{
"type":"Error",
"code":404,
"message":"Not Found"
}
}
我想GSON转换为正确的类型取决于上文所述error
键/值对的存在。我能想到的最实用的方法如下,但我很好奇,如果有更好的方法。
final String response = client.get("http://www.example.com/user.json?id=1");
final Gson gson = new Gson();
try {
final UserEntity user = gson.fromJson(response, UserEntity.class);
// do something with user
} catch (final JsonSyntaxException e) {
try {
final ErrorEntity error = gson.fromJson(response, ErrorEntity.class);
// do something with error
} catch (final JsonSyntaxException e) {
// handle situation where response cannot be parsed
}
}
这其实只是伪代码,但因为在第一个catch条件,我不知道如何测试的关键error
是否存在于JSON响应。所以我想我的问题是双重的:
- 我可以/我怎么能用GSON来测试一个密钥的存在,并根据这个来决定如何解析?
- 这是别人在类似的情况下与GSON做的事,还是有更好的办法?
错误代码更改应该很简单,我如何配置我的Web应用程序。我将不得不四处弄清楚是否删除结果JSON中的“错误”键或使用类似上面的类似的方法对我来说会更好。非常感谢您的详细解答,我非常感谢! – 2011-01-31 07:03:41