dimanche 28 juin 2015

Parsing json with gson and java

I am struggling to parse a json output with Java and gson, but I am really stuck.

I would appreciate any suugestion.

This is my sample JSON file:

{
"sportId": 29,
"last": 26142386,
"league": [
    {
        "id": 1833,
        "events": [
            {
                "id": 383911973,
                "starts": "2015-01-22T21:00:00Z",
                "home": "America de Natal",
                "away": "Barras",
                "rotNum": "901",
                "liveStatus": 0,
                "status": "O",
                "parlayRestriction": 0
            },
            {
                "id": 383911974,
                "starts": "2015-01-22T21:00:00Z",
                "home": "Baraunas RN",
                "away": "ASSU RN",
                "rotNum": "904",
                "liveStatus": 0,
                "status": "O",
                "parlayRestriction": 0
            }
        ]
    }
  ]
}

My target is to a make a 2-dimensional array (or something similar) of the form:

leagueId, eventId, home, away
------------------------------
   1         1       a    b
   .         .       .    .
   .         .       .    .
  etc       etc     etc   etc

in order to insert the data in a MYSQL table.

I have write the following classes:

public class Fixtures {
int last;
int sportId;
ArrayList<Leagues> league = new ArrayList<Leagues>();

public ArrayList<Leagues> getListOfLeagues() {  
    return league;  
}  

public int getSportId(){
    return sportId;
}

 public int getLast(){
    return last;
 }

}

public class Leagues {
int id;
ArrayList<Events> events;

public int getLeagueId(){
    return id;
}

 public ArrayList<Events> getListOfEvents() {  
    return events;  
 }  

}

public class Events {
int id;
String home;
String away;


public int getEventId(){
    return id;
}

public String getHome() {  
    return home;  
}  

 public String getAway() {
    return away;
 }


}

and

Gson gson = new GsonBuilder().create();             
Fixtures fixture = gson.fromJson(jsonsource, Fixtures.class);
System.out.println(fixture.getSportId());
System.out.println(fixture.getLast());

ArrayList<Leagues> Leagues = fixture.getListOfLeagues();

Dont know how to proceed :(

Aucun commentaire:

Enregistrer un commentaire