Arrays OpenWeatherMapAPI预测访问阵列
所以我正试图搞乱OpenWeatherMapAPI,我不知道如何访问一些数据 这里是我访问API的代码片段Arrays OpenWeatherMapAPI预测访问阵列,arrays,android-studio,openweathermap,Arrays,Android Studio,Openweathermap,所以我正试图搞乱OpenWeatherMapAPI,我不知道如何访问一些数据 这里是我访问API的代码片段 public void find_forecast () { String url = "http://api.openweathermap.org/data/2.5/forecast?id=4466033&appid=e4c641dd837c7947a127972091185dad&units=imperial"; JsonObjectRequ
public void find_forecast () {
String url = "http://api.openweathermap.org/data/2.5/forecast?id=4466033&appid=e4c641dd837c7947a127972091185dad&units=imperial";
JsonObjectRequest jor = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray array = response.getJSONArray("list");
JSONObject object = array.getJSONObject(1);
String description = object.getString("dt");
day1.setText(description);
如您所见,上面的代码将返回dt的值,即1524614400。但我试图在“天气”下访问“描述”,在本例中是小雨。我想我不知道如何访问数组中的数组,尽管我甚至不能让“temp”返回到“main”下,尽管我可以让“main”返回它下面的所有内容
谢谢你的帮助
Max不确定Android,但在Javascript中,我会尝试以下方法:
var dataObj = {
"cod": "200",
"message": 0.0042,
"cnt": 40,
"list": [
{
"dt": 1524614400,
"main": {
"temp": 63.77,
"temp_min": 63.77,
"temp_max": 64.26,
"pressure": 1017.45,
"sea_level": 1021.4,
"grnd_level": 1017.45,
"humidity": 97,
"temp_kf": -0.27
},
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10n"
}
]
}
]
}
console.log(dataObj.list[0].weather[0].description);
我终于明白了。代码在下面
String array = response.getJSONArray("list").getJSONObject(0).getJSONArray("weather").getJSONObject(0).getString("description");
day1.setText(array);
这将从上面的API代码返回小雨
String array = response.getJSONArray("list").getJSONObject(0).getJSONArray("weather").getJSONObject(0).getString("description");
day1.setText(array);