Skip to content

Instantly share code, notes, and snippets.

@haldun
Created February 20, 2015 13:43
Show Gist options
  • Save haldun/32b9dd5926fe65d0ac01 to your computer and use it in GitHub Desktop.
Save haldun/32b9dd5926fe65d0ac01 to your computer and use it in GitHub Desktop.
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
class Response {
protected String message;
protected int status;
public String getMessage() {
return message;
}
public int getStatus() {
return status;
}
}
class ScoreResponse extends Response {
ScoreInner score;
public ScoreInner getScore() {
return score;
}
@Override
public String toString() {
return "ScoreResponse{" +
"message=" + message +
", status=" + status +
", score=" + score +
'}';
}
private static class ScoreInner {
private int success;
private int fail;
private int points;
@Override
public String toString() {
return "ScoreInner{" +
"success=" + success +
", fail=" + fail +
", points=" + points +
'}';
}
}
}
public class Main {
public static void main(String[] args) {
String json = "{score: {\n" +
" success: 5,\n" +
" fail: 2,\n" +
" points: 10\n" +
" },\n" +
" message: \"message\",\n" +
" status: 200\n" +
" }";
Gson gson = new GsonBuilder().create();
ScoreResponse scoreResponse = gson.fromJson(json, ScoreResponse.class);
System.out.println(scoreResponse.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment