使用改造及GSON与使用改造,可以选用下列API返回动态顶层JSON
林的API:使用改造及GSON与使用改造,可以选用下列API返回动态顶层JSON
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>
得到它自动解析并将日期设置为地图中的关键字。在围绕SO的一些答案中提出了这种方法。
我试图做同样在这种情况下,代替上述类看起来像这样:
public class NearEarthObjects { private Map<String, Observation> observation = new HashMap<>();
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
}
不幸的是,解决这个方法这个时候似乎并没有被工作正常。地图正在返回空白。什么可能是这个问题?什么是最好的方式来构建我的模型,以正确地解析返回的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
]
如果要分析动态密钥。您可以使用此链接: How to parse a dynamic JSON key in a Nested JSON result?
以上是 使用改造及GSON与使用改造,可以选用下列API返回动态顶层JSON 的全部内容, 来源链接: utcz.com/qa/263343.html