Michael Blala
Michael Blala

Reputation: 51

Get Array and it's objects from JSON using GSON

I have this string:

{"cod":"200","message":0.0049,"cnt":40,"list":[{"dt":1549346400,"main":{"temp":-1.04,"temp_min":-1.04,"temp_max":-1.04,"pressure":1023.46,"sea_level":1025.98,"grnd_level":1023.46,"humidity":92,"temp_kf":0},"weather":[{"id":600,"main":"Snow","description":"небольшой снегопад","icon":"13n"}],"clouds":{"all":80},"wind":{"speed":3.26,"deg":226.502},"rain":{},"snow":{"3h":1.485},"sys":{"pod":"n"},"dt_txt":"2019-02-05 06:00:00"},{"dt":1549357200,"main":{"temp":-1.04,"temp_min":-1.04,"temp_max":-1.04,"pressure":1023.78,"sea_level":1026.44,"grnd_level":1023.78,"humidity":95,"temp_kf":0},"weather":[{"id":600,"main":"Snow","description":"небольшой снегопад","icon":"13d"}],"clouds":{"all":80},"wind":{"speed":5.32,"deg":243.5},"rain":{},"snow":{"3h":1.115},"sys":{"pod":"d"},"dt_txt":"2019-02-05 09:00:00"}],"city":{"id":536203,"name":"Sankt-Peterburg","coord":{"lat":59.9167,"lon":30.25},"country":"RU"}}

it's a JSON and I did this classes to get data

public class FiveDaysWeather {

    private long dt;
    private List<WeatherTomorrow> weather = null;
    private Temp main;
    private Wind wind;

    public long getDt() {
        return dt;
    }

    public void setDt(long dt) {
        this.dt = dt;
    }

    public List<WeatherTomorrow> getWeatherTomorrow() {
        return weather;
    }

    public void setWeatherTomorrow(List<WeatherTomorrow> weather) {
        this.weather = weather;
    }

    public Temp getTemp() {
        return main;
    }

    public void setTemp(Temp main) {
        this.main = main;
    }

    public Wind getWind() {
        return wind;
    }

    public void setWind(Wind wind) {
        this.wind = wind;
    }
}

public class WeatherTomorrow {
    private String icon;
    private String description;
    private String main;
    private long id;

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getMain() {
        return main;
    }

    public void setMain(String main) {
        this.main = main;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
}

So from this string I need too get data about weather (temp, humidity, description and wind), which is in Array "list" in first dt "1549346400". I did a JSON array and got "list" as JSONArray. Yhan I got from this list "dt 1549346400" as JSONObject and used getters to get data.

Now I need to do the same thing using google GSON. Read the guide on github but still dont understand how to get 0 index data from JSON.

Upvotes: 1

Views: 601

Answers (1)

Mebin Joe
Mebin Joe

Reputation: 2209

String jsonStr = "{}";

Gson gson = new Gson();
JsonElement element = gson.fromJson (jsonStr, JsonElement.class);
JsonObject jsonObj = element.getAsJsonObject();

Upvotes: 1

Related Questions