1

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?

Orbit
  • 2,985
  • 9
  • 49
  • 106

1 Answers1

0

I think the easy way to parse your Json data inside object near_earth_objects that structuring your json returned like that:

"near_earth_objects":[  
   {  
      "date":"2016-11-07",
      "data":[  
         {  
            "links":{  
               "self":"https://api.nasa.gov/neo/rest/v1/neo/3758255?api_key=DEMO_KEY"
            },
            "neo_reference_id":"3758255",
            "name":"(2016 QH44)",
            "nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3758255",
            "absolute_magnitude_h":22.381,
            "estimated_diameter":{  
               "kilometers":{  
                  "estimated_diameter_min":0.0887881438,
                  "estimated_diameter_max":0.1985363251
               },
               "meters":{  
                  "estimated_diameter_min":88.7881437713,
                  "estimated_diameter_max":198.5363250687
               },
               "miles":{  
                  "estimated_diameter_min":0.0551703777,
                  "estimated_diameter_max":0.1233647148
               },
               "feet":{  
                  "estimated_diameter_min":291.2996936107,
                  "estimated_diameter_max":651.3659167384
               }
            },
            "is_potentially_hazardous_asteroid":false,
            "close_approach_data":[  
               {  
                  "close_approach_date":"2016-11-07",
                  "epoch_date_close_approach":1478505600000,
                  "relative_velocity":{  
                     "kilometers_per_second":"9.9505291907",
                     "kilometers_per_hour":"35821.9050865416",
                     "miles_per_hour":"22258.3387466903"
                  },
                  "miss_distance":{  
                     "astronomical":"0.1045395934",
                     "lunar":"40.6659011841",
                     "kilometers":"15638901",
                     "miles":"9717563"
                  },
                  "orbiting_body":"Earth"
               }
            ]
         },
         {  
            "links":{  
               "self":"https://api.nasa.gov/neo/rest/v1/neo/3758255?api_key=DEMO_KEY"
            },
            "neo_reference_id":"3758255",
            "name":"(2016 QH44)",
            "nasa_jpl_url":"http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3758255",
            "absolute_magnitude_h":22.381,
            "estimated_diameter":{  
               "kilometers":{  
                  "estimated_diameter_min":0.0887881438,
                  "estimated_diameter_max":0.1985363251
               },
               "meters":{  
                  "estimated_diameter_min":88.7881437713,
                  "estimated_diameter_max":198.5363250687
               },
               "miles":{  
                  "estimated_diameter_min":0.0551703777,
                  "estimated_diameter_max":0.1233647148
               },
               "feet":{  
                  "estimated_diameter_min":291.2996936107,
                  "estimated_diameter_max":651.3659167384
               }
            },
            "is_potentially_hazardous_asteroid":false,
            "close_approach_data":[  
               {  
                  "close_approach_date":"2016-11-07",
                  "epoch_date_close_approach":1478505600000,
                  "relative_velocity":{  
                     "kilometers_per_second":"9.9505291907",
                     "kilometers_per_hour":"35821.9050865416",
                     "miles_per_hour":"22258.3387466903"
                  },
                  "miss_distance":{  
                     "astronomical":"0.1045395934",
                     "lunar":"40.6659011841",
                     "kilometers":"15638901",
                     "miles":"9717563"
                  },
                  "orbiting_body":"Earth"
               }
            ]
         }
      ]
   }
 // The other objects 
]

if you want to parse dynamic key. You can use this link: How to parse a dynamic JSON key in a Nested JSON result?

Community
  • 1
  • 1
RoShan Shan
  • 2,924
  • 1
  • 18
  • 38
  • Yes, how it is currently structured is wrong, but I do not own the API. In one of the answers in the link you mentioned, they also tried to use a `Map`, though as stated this doesn't seem to be working in my scenario. I could manually parse the JSON but I'd like Gson to handle as much as possible. – Orbit Nov 02 '16 at 04:46
  • U can parse `JSON` manually, by make JSON as String and use `JSONObject` to do it. [Link ] (http://stackoverflow.com/questions/7304002/how-to-parse-a-dynamic-json-key-in-a-nested-json-result) – RoShan Shan Nov 07 '16 at 02:33