Created
June 14, 2015 03:39
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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