2

First off, I want to say that I extensively searched how to do this, using the advice from (at the very least) the following links:

Parsing JSON array that contain objects with different attributes with Retrofit https://medium.com/@dds861/json-parsing-using-retrofit-and-recycleview-2300d9fdcf15 http://instinctcoder.com/android-studio-parsing-json-using-retrofit/

How to parse multiple JSON arrays inside a JSON object using Gson?

Using Retrofit to access JSON arrays

How to parse list of JSON objects surrounded by [] using Retrofit and GSON?

However, all of the above links show how to parse JSON objects/arrays with the same type of objects, or objects within just one array.

Here is the specific JSON data I'm working with:

{  
  "coord":{  
  "lon":-83.92,
  "lat":34.08
},
"weather":[  
  {  
     "id":500,
     "main":"Rain",
     "description":"light rain",
     "icon":"10d"
  }
],
"base":"stations",
"main":{  
  "temp":292.16,
  "pressure":1011,
  "humidity":64,
  "temp_min":290.15,
  "temp_max":294.15
 },
"visibility":16093,
"wind":{  
  "speed":3.6,
  "deg":290,
  "gust":7.7
 },
"clouds":{  
  "all":90
 },
"dt":1524609300,
"sys":{  
  "type":1,
  "id":809,
  "message":0.0057,
  "country":"US",
  "sunrise":1524567171,
  "sunset":1524615295
 },
"id":420007383,
"name":"Athens",
"cod":200
  }

I converted it all to pojo using jsonschema2pojo.org. My question is, how can I use retrofit to access a variable like "main" inside the Weather object, when I already have a "main" object in the JSON array? For that matter, how can I access any of the inner objects using Retrofit?

I have made the variables all serializable, and experimented using static classes for the objects within the array.

As an example, say I want to retrieve "id" within the "weather" object. My JSONResponse class looks like this:

public class JSONResponse {
@SerializedName("weather")
@Expose
private List<Weather> weather = null;
@SerializedName("main")
@Expose
private Main main;
@SerializedName("wind")
@Expose
private Wind wind;
@SerializedName("clouds")
@Expose
private Clouds clouds;
@SerializedName("sys")
@Expose
private Sys sys;
@SerializedName("name")
@Expose
private String name;

//getter and setter methods
}

And my Weather class looks like this: public class Weather {

    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("main")
    @Expose
    //This describes what the weather is like, e.g. clear or cloudy
    private String main;
    @SerializedName("description")
    @Expose
    private String description;
    @SerializedName("icon")
    @Expose
    private String icon;

    public Integer getId() {
        return id;
    }

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

    public String getMain() {
        return main;
    }

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

    public String getDescription() {
        return description;
    }

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

    public String getIcon() {
        return icon;
    }

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

}

How would I retrieve that data using Retrofit in my main activity? Would I be better off just using RXJava without Retrofit? If so, how can I go about doing that? Thanks in advance to anyone who has the patience to answer this question.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.