I am learning Google GSON for my Android project, which will request JSON from my web server. The returned JSON will be either ...
1) A successful response of a known type (for example, the class "User"):
{ "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.) An unsuccessful answer that will always have the same basic structure below.
{ "error":{ "type":"Error", "code":404, "message":"Not Found" } }
I would like GSON to convert to the correct type depending on the presence of the error key / value pair above. The most practical way I can do is as follows, but I'm curious if there is a better way.
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 } }
This is really just pseudo code, though, since in the first catch state I am not sure how to check if the error key exists in the JSON response. Therefore, I think my question is twofold:
- Can I / how can I use GSON to verify the key exists and decide how to understand it?
- Is this what others in a similar situation do with GSON, or is there a better way?
source share