Skip to content

Instantly share code, notes, and snippets.

@bearprada
Created June 14, 2015 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bearprada/0cbcdf39c70f63eaca3f to your computer and use it in GitHub Desktop.
Save bearprada/0cbcdf39c70f63eaca3f to your computer and use it in GitHub Desktop.
the sample code to use JsonDeserializer to handle the nest/complicate JSON format
/**
* Example :
* input : {server: { result : { data: { a:1, b:2 }} }}
**/
public class MyDeserializer implements JsonDeserializer<MyClass> {
public MyClass deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonElement j = json.getAsJsonObject().get("server").getAsJsonObject().get("result").getAsJsonObject().get("data");
MyClass clz = new MyClass();
clz.data = context.deserialize(j, MyData.class);
return clz;
}
}
public class MyClass {
public MyData data;
}
public class MyData {
@Serialize("a")
public int a;
@Serialize("b")
public int b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment