Gson Parsing Unspecified Type Variable
Solution 1:
You need to scrape the JSON response before trying to deserialize it into your Response
Java object. You can make use of Java org.json parser to verify that service
object actually exists and fix it otherwise.
String json ="{\"service\":{\r\n"+" \"description\":null,\r\n"+" \"name\":\"Base\",\r\n"+" \"id\":\"4c7a90410529\"\r\n"+"}}";
String json2 ="{\"service\":\"\"}";
JSONObject root = new JSONObject(json);
// JSONObject root = new JSONObject(json2);if (root.optJSONObject("service") == null) {
root.put("service", new JSONObject());
}
Gson gson = new Gson();
Response response = gson.fromJson(root.toString(), Response.class);
System.out.println(response.getService());
Output :
// for JSONObject root = new JSONObject(json);
Service [id=4c7a90410529, name=Base, description=null]
// for JSONObject root = new JSONObject(json2);
Service [id=null, name=null, description=null]
Secondly, Gson
is smart enough to do simple conversions like String
to Integer
etc. So, deserializing such JSON properties shouldn't give you any troubles.
System.out.println(gson.fromJson("10", Integer.class)); // 10
System.out.println(gson.fromJson("\"20\"", Integer.class)); // 20
Solution 2:
If you want to stick with strictly gson
you can provide a custom deserializer. Since we know that service
is either a property of the base json
string or embedded within some other property
, we can use the deserializer to step-wise parse out the offending components and handle them accordingly.
publicclassMyJsonDeserializerimplementsJsonDeserializer<YourParsedData> {
@Overridepublic YourParsedData deserialize(final JsonElement je, final Type type, final JsonDeserialization Context jdc)throws JsonParseException
{
finalJsonObjectobj= je.getAsJsonObject(); //our original full json stringfinalJsonElementserviceElement= obj.get("service");
//here we provide the functionality to handle the naughty element. It seems emtpy string is returned as a JsonPrimitive... so one optionif(serviceElement instanceOf JsonPrimitive)
{
//it was empty do something
}
return YourParsedData.create(); //provide the functionality to take in the parsed data
}
}
The custom deserializer would be called as follows:
final Gson gson = newGsonBuilder().registerTypeAdapter(YourParsedData.class, newMyJsonDeserializer()).create();
gson.fromJson("{service: ''}", YourParsedData.class);
I typed all this up so if I missed some syntax my apologies.
Solution 3:
Your json is invalid and any Json parser wouldn't be able to parse a syntactically incorrect json:
"service":{"description":null,"name":"Base","id":"4c7a90410529"}
should be encapsulated in curly braces as mentioned here:
{"service":{"description":null,"name":"Base","id":"4c7a90410529"}}
Solution 4:
A json structure is enclosed within {}
. Your response seems to be missing that. You can manually append {
and }
at the beginning and end of the string to make it into a valid json structure.
Once this is done, you can use Gson to parse your json response normally.
What is the best practice to parse such response?
Use a good enough Json parser. That's more than enough. And try to have a class representing the exact same Structure as the response to avoid parsing the json responses level by level, manually.
Post a Comment for "Gson Parsing Unspecified Type Variable"