我使用 Retrofit 访问以下 api:
https://api.nasa.gov/neo/rest/v1/feed?api_key=DEMO_KEY
near_earth_objects
对象包含多个数组,每个数组都有一个代表日期的键。如果您访问不同的日期,该值显然会发生变化。
像往常一样,我根据返回的 JSON 结构定义了 POJO。以下是我的主要响应类:
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
}
NearEarthObjects
类如下所示:
public class NearEarthObjects {
private List<Observation> observation = new ArrayList<>();
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
//getters and setters
}
我之前遇到过这个问题,并且能够使用 Map<String, SomeCustomModel>
让它自动解析并将日期设置为 map 中的键。在围绕 SO 的一些答案中建议了这种方法。
在这种情况下我尝试做同样的事情,将上述类替换为如下所示:
public class NearEarthObjects {
private Map<String, Observation> observation = new HashMap<>();
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
}
不幸的是,这次这个方法似乎没有按预期工作。 map 返回为空。可能是什么问题?构建模型以正确解析返回的 JSON 的最佳方法是什么?
最佳答案
我认为解析对象near_earth_objects
内的Json
数据的简单方法是构造你的json返回如下:
"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
]
如果你想解析动态 key 。您可以使用此链接: How to parse a dynamic JSON key in a Nested JSON result?
关于java - 使用 Retrofit 和 Gson 以及返回动态顶级 JSON 的 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40372054/