Java 在返回动态顶级JSON的api中使用改型和Gson
Im使用改装访问以下api:Java 在返回动态顶级JSON的api中使用改型和Gson,java,android,json,gson,retrofit2,Java,Android,Json,Gson,Retrofit2,Im使用改装访问以下api: near_earth_objects对象包含多个数组,每个数组都有一个表示日期的键。如果访问不同的日期,此值会明显更改 像往常一样,我根据返回的JSON结构定义了POJO。以下是我的主要回答类: public class AsteroidResponse { private Links links; @SerializedName("element_count") private Integer elementCount; @
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
}
我以前遇到过这个问题,并且能够使用Map
让它自动解析并将日期设置为Map中的键。这一方法在一些关于SO的答案中得到了建议
在这种情况下,我也尝试过这样做,将前面提到的类替换为如下:
public class NearEarthObjects {
private Map<String, Observation> observation = new HashMap<>();
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
}
公共类近地天体{
私有映射观察=新HashMap();
私有映射additionalProperties=new HashMap();
}
不幸的是,这一次,这种方法似乎并不像预期的那样有效。地图将被返回为空。可能是什么问题?构造模型以正确解析返回的JSON的最佳方法是什么?我认为解析对象
近地天体内的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
]
如果您想解析动态键。您可以使用此链接:
我认为解析对象近地天体中的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
]
如果您想解析动态键。您可以使用此链接:
是的,它当前的结构是错误的,但我不拥有API。在您提到的链接中的一个答案中,他们还尝试使用地图
,尽管如前所述,这在我的场景中似乎不起作用。我可以手动解析JSON,但我希望Gson尽可能多地处理。你可以手动解析JSON
,方法是将JSON设置为字符串,并使用JSONObject
来完成。[Link]()是的,它当前的结构是错误的,但我没有API。在您提到的链接中的一个答案中,他们还尝试使用地图
,尽管如前所述,这在我的场景中似乎不起作用。我可以手动解析JSON,但我希望Gson尽可能多地处理。你可以手动解析JSON
,方法是将JSON设置为字符串,并使用JSONObject
来完成。[链接]()