Im using Retrofit to access the following api:
https://api.nasa.gov/neo/rest/v1/feed?api_key=DEMO_KEY
The near_earth_objects
object contains multiple arrays each with a key representing a date. This value obviously changes if you access a different date.
As usual, I defined my POJOs according to the returned JSON structure. The following is my main response class:
public class AsteroidResponse {
private Links links;
@SerializedName("element_count")
private Integer elementCount;
@SerializedName("near_earth_objects")
private NearEarthObjects nearEarthObjects;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
//getters and setters
}
The NearEarthObjects
class looks like the following:
public class NearEarthObjects {
private List<Observation> observation = new ArrayList<>();
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
//getters and setters
}
I've run into this issue before, and was able to use a Map<String, SomeCustomModel>
to get it to automatically parse and set the the date as a key in the map. This method is suggested in a few answers around SO.
I tried to do the same in this situation, replacing the aforementioned class to look like this:
public class NearEarthObjects {
private Map<String, Observation> observation = new HashMap<>();
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
}
Unfortunately, this time around this method does not seem to be working as expected. The map is being returned empty. What might be the issue? What would be the best way to structure my models to properly have the returned JSON parsed?