How to parse JsonObject to List<MyClass> using gson

942 views Asked by At

I need a list of objects and I'm having problems to get it. I'm pretty new using this so someone who can help me?

I'm using volley to get a jsonObject and then I need convert it(I saw the best wa to do this is with gson). Below you can see how looks an example of my json.

{
network: {
    company: "JCDecaux",
    href: "/v2/networks/dublinbikes",
    id: "dublinbikes",
    location: {
        city: "Dublin",
        country: "IE",
        latitude: 53.3498053,
        longitude: -6.2603097
    },
    name: "dublinbikes",
    stations: [
        {
            empty_slots: 37,
            extra: {
                address: "Fitzwilliam Square East",
                bonus: false,
                connected: "1",
                last_update: "1434047944",
                open: true,
                slots: 40,
                ticket: true,
                uid: 89
            },
            free_bikes: 3,
            id: "153ff4dfb7bd8912ef91c10849129c2e",
            latitude: 53.335211,
            longitude: -6.2509,
            name: "Fitzwilliam Square East",
            timestamp: "2015-06-11T18:41:31.11Z"
        },
        {
            empty_slots: 0,
            extra: {
                address: "Portobello Harbour",
                bonus: false,
                connected: "1",
                last_update: "1434047764",
                open: true,
                slots: 30,
                ticket: false,
                uid: 34
            },
            free_bikes: 30,
            id: "3c0cfd547a142bb651280991a412bcbe",
            latitude: 53.330362,
            longitude: -6.265163,
            name: "Portobello Harbour",
            timestamp: "2015-06-11T18:41:31.15Z"
        },

... ect

Station class

public class Station {
    public Station(){}

    public String StationName;
    public String Distance;
    public String Slots;
    public String Bikes;
    public String LastUpdate;

    //Getters and Setters ...
}

Below you can see what I have done so far..

 //stations
 JSONObject jsonNetwork = new JSONObject(response.getString("network"));

   Type listType = new TypeToken<ArrayList<Station>>() {
                }.getType();
   List<Station> yourClassList = new Gson().fromJson(jsonNetwork, listType);

But I don't know how to parse all this and avoid the data I dont need and also the function Gson().fromJson needs and JsonArray and what I have is a JsonObject

Thanks for your time!

2

There are 2 answers

0
Jayesh Elamgodil On BEST ANSWER

If all that you are looking for are the stations then I think you should do the following:

//stations
    JSONObject jsonNetwork = new JSONObject(response.getString("network"));
    JSONArray stationsArray = jsonNetwork.getJSONArray("stations");

Now, you should pass this stationsArray variable to the fromJson method. Also, the variable names of your Station class should be equal to the key names that you want to extract from your json. There is a good example in the following link:

http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

5
spgodara On

Use below code

Gson gson = new Gson();
YourClass obj = gson.fromJson(jsonObject.toString(), YourClass.class);