Skip to content

How to Decode JSON with Google GSON Library

31/03/2011
String data ="[ { \"away\": \"Ankaragücü\", \"date\": \" FT - 21/03/11\", \"home\": \"Bursaspor\", \"score\": \"0:0 \" }, { \"away\": \"Sivasspor\", \"date\": \" FT - 20/03/11\", \"home\": \"Bucaspor\", \"score\": \"0:4 (0:1) \" }, { \"away\": \"Trabzonspor\", \"date\": \" FT - 20/03/11\", \"home\": \"Gençlerbirligi\", \"score\": \"1:2 (1:0) \" }, { \"away\": \"Karabukspor\", \"date\": \" FT - 20/03/11\", \"home\": \"Eskisehirspor\", \"score\": \"1:0 (1:0) \" }, { \"away\": \"Kayserispor\", \"date\": \" FT - 19/03/11\", \"home\": \"Besiktas\", \"score\": \"4:2 (0:1) \" } ]";
    
	  Gson gson = new Gson();
	  
	  Type type = new TypeToken<List<Table>>(){}.getType();
	  List<Table> table = gson.fromJson(data, type);

      for (Table tab : table) {
          System.out.println("Away Team:  " + tab.getAway());
      }
    }
public class Table {
String away;
String date;
String home;
String score;

public String getAway() {
	return away;
}
public void setAway(String away) {
	this.away = away;
}
public String getDate() {
	return date;
}
public void setDate(String date) {
	this.date = date;
}
public String getHome() {
	return home;
}
public void setHome(String home) {
	this.home = home;
}
public String getScore() {
	return score;
}
public void setScore(String score) {
	this.score = score;
}

}

Advertisements

From → Java Examples

5 Comments
  1. VooDooNOFX permalink

    This example would make more sense, and be a lot more helpful if you included the Table.java definition also!

  2. Nice tutorial and nice library.I am working with DataTable and I was searching a cool library like this..

  3. eeeh your example is very chupapijeiting in the bañera with proud!!! very useful to go cojeiting your hermana!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: