我正在尝试从url解析json代码,但我无法看到我的错误,因为我没有得到任何结果。这是我的代码:
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
jsonStr = sh.makeServiceCall("http://api.apixu.com/v1/forecast.json?key=93a1531ba64540f7a41180856163011&q=bilbao&days=1", ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONObject fore = jsonObj.getJSONObject("forecast");
// Getting JSON Array node
eventos = fore.getJSONArray("forecastday");
porhoraca = fore.getJSONArray("hour");
for (int i = 0; i < porhoraca.length(); i++) {
JSONObject c = porhoraca.getJSONObject(i);
String hora = c.getString(TAG_TIME);
String tempec = c.getString(TAG_TEMP_C);
String sensacion = c.getString(TAG_FEELSLIKE_C);
JSONObject condi = c.getJSONObject(TAG_CONDITION);
String texto = condi.getString(TAG_TEXT);
String icono = condi.getString(TAG_ICON);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put("nombre", hora);
contact.put("hora", tempec);
contact.put("nombre_lugar", sensacion);
contact.put("coordenadas", texto);
contact.put("info", icono);
// adding contact to contact list
eventosList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
我只想从有json的url中获取这些对象。有些我做错了。也许我没有正确阅读json对象和Json数组。有人可以说我的错误在哪里?感谢
Json代码在这里:
http://api.apixu.com/v1/forecast.json?key=93a1531ba64540f7a41180856163011&q=bilbao&days=1
答案 0 :(得分:2)
我认为您的问题是hour
是forecastday
中的数组,而不是forecast
中当前处理它的数据。
我希望有所帮助!
答案 1 :(得分:1)
最后,我实现了你的代码,调试它并在eventosList得到最终结果:
[{nombre_lugar=2.0, coordenadas=Clear, hora=2.7, nombre=2017-01-05 00:00, info=//cdn.apixu.com/weather/64x64/night/113.png}, {nombre_lugar=1.8, coordenadas=Clear, hora=2.5, nombre=2017-01-05 01:00, info=//cdn.apixu.com/weather/64x64/night/113.png}, {nombre_lugar=1.6, coordenadas=Clear, hora=2.4, nombre=2017-01-05 02:00, info=//cdn.apixu.com/weather/64x64/night/113.png}, {nombre_lugar=1.4, coordenadas=Clear, hora=2.2, nombre=2017-01-05 03:00, info=//cdn.apixu.com/weather/64x64/night/113.png}, {nombre_lugar=1.0, coordenadas=Clear, hora=2.0, nombre=2017-01-05 04:00, info=//cdn.apixu.com/weather/64x64/night/113.png}, {nombre_lugar=0.7, coordenadas=Clear, hora=1.7, nombre=2017-01-05 05:00, info=//cdn.apixu.com/weather/64x64/night/113.png}, {nombre_lugar=0.3, coordenadas=Clear, hora=1.5, nombre=2017-01-05 06:00, info=//cdn.apixu.com/weather/64x64/night/113.png}, {nombre_lugar=1.0, coordenadas=Clear, hora=2.4, nombre=2017-01-05 07:00, info=//cdn.apixu.com/weather/64x64/night/113.png}, {nombre_lugar=1.8, coordenadas=Clear, hora=3.4, nombre=2017-01-05 08:00, info=//cdn.apixu.com/weather/64x64/night/113.png}, {nombre_lugar=2.5, coordenadas=Sunny, hora=4.3, nombre=2017-01-05 09:00, info=//cdn.apixu.com/weather/64x64/day/113.png}, {nombre_lugar=3.9, coordenadas=Sunny, hora=5.7, nombre=2017-01-05 10:00, info=//cdn.apixu.com/weather/64x64/day/113.png}, {nombre_lugar=5.4, coordenadas=Sunny, hora=7.1, nombre=2017-01-05 11:00, info=//cdn.apixu.com/weather/64x64/day/113.png}, {nombre_lugar=6.8, coordenadas=Sunny, hora=8.5, nombre=2017-01-05 12:00, info=//cdn.apixu.com/weather/64x64/day/113.png}, {nombre_lugar=7.6, coordenadas=Sunny, hora=9.1, nombre=2017-01-05 13:00, info=//cdn.apixu.com/weather/64x64/day/113.png}, {nombre_lugar=8.3, coordenadas=Sunny, hora=9.7, nombre=2017-01-05 14:00, info=//cdn.apixu.com/weather/64x64/day/113.png}, {nombre_lugar=9.1, coordenadas=Sunny, hora=10.3, nombre=2017-01-05 15:00, info=//cdn.apixu.com/weather/64x64/day/113.png}, {nombre_lugar=8.5, coordenadas=Sunny, hora=9.3, nombre=2017-01-05 16:00, info=//cdn.apixu.com/weather/64x64/day/113.png}, {nombre_lugar=7.9, coordenadas=Sunny, hora=8.3, nombre=2017-01-05 17:00, info=//cdn.apixu.com/weather/64x64/day/113.png}, {nombre_lugar=7.3, coordenadas=Clear, hora=7.3, nombre=2017-01-05 18:00, info=//cdn.apixu.com/weather/64x64/night/113.png}, {nombre_lugar=6.3, coordenadas=Clear, hora=6.3, nombre=2017-01-05 19:00, info=//cdn.apixu.com/weather/64x64/night/113.png}, {nombre_lugar=5.3, coordenadas=Clear, hora=5.3, nombre=2017-01-05 20:00, info=//cdn.apixu.com/weather/64x64/night/113.png}, {nombre_lugar=4.3, coordenadas=Clear, hora=4.3, nombre=2017-01-05 21:00, info=//cdn.apixu.com/weather/64x64/night/113.png}, {nombre_lugar=1.5, coordenadas=Clear, hora=2.5, nombre=2017-01-05 22:00, info=//cdn.apixu.com/weather/64x64/night/113.png}, {nombre_lugar=-1.3, coordenadas=Clear, hora=0.6, nombre=2017-01-05 23:00, info=//cdn.apixu.com/weather/64x64/night/113.png}]
soultion就是这个试试这个,你会得到完整的结果:
try {
JSONObject jsonObj = new JSONObject(response);
JSONObject fore = jsonObj.getJSONObject("forecast");
// Getting JSON Array node
JSONArray eventos = fore.getJSONArray("forecastday");
JSONArray porhoraca = eventos.getJSONObject(0).getJSONArray("hour");
for (int i = 0; i < porhoraca.length(); i++) {
JSONObject c = porhoraca.getJSONObject(i);
String hora = c.getString("time");
String tempec = c.getString("temp_c");
String sensacion = c.getString("feelslike_c");
JSONObject condi = c.getJSONObject("condition");
String texto = condi.getString("text");
String icono = condi.getString("icon");
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put("nombre", hora);
contact.put("hora", tempec);
contact.put("nombre_lugar", sensacion);
contact.put("coordenadas", texto);
contact.put("info", icono);
// adding contact to contact list
eventosList.add(contact);
}
Log.d("testttttttt", eventosList.toString());
} catch (JSONException e) {
e.printStackTrace();
}
答案 2 :(得分:-1)
这是你的问题的完整解决方案,你必须在JsonObject中传递响应,代码如下
try {
JSONObject object = new JSONObject(response);
JSONObject object_location = object.getJSONObject("location");
JSONObject object_current = object.getJSONObject("current");
JSONObject object_forecast = object.getJSONObject("forecast");
String name = object_location.getString("name");
String region = object_location.getString("region");
String country = object_location.getString("country");
String lat = object_location.getString("lat");
String lon = object_location.getString("lon");
String tz_id = object_location.getString("tz_id");
String localtime_epoch = object_location.getString("localtime_epoch");
String localtime = object_location.getString("localtime");
String last_updated_epoch = object_current.getString("last_updated_epoch");
String last_updated = object_current.getString("last_updated");
String temp_c = object_current.getString("temp_c");
String temp_f = object_current.getString("temp_f");
String is_day = object_current.getString("is_day");
String wind_mph = object_current.getString("wind_mph");
String wind_kph = object_current.getString("wind_kph");
String wind_degree = object_current.getString("wind_degree");
String wind_dir = object_current.getString("wind_dir");
String pressure_mb = object_current.getString("pressure_mb");
String pressure_in = object_current.getString("pressure_in");
String precip_mm = object_current.getString("precip_mm");
String precip_in = object_current.getString("precip_in");
String humidity = object_current.getString("humidity");
String cloud = object_current.getString("cloud");
String feelslike_c = object_current.getString("feelslike_c");
String feelslike_f = object_current.getString("feelslike_f");
JSONObject object_condition = object_current.getJSONObject("condition");
String text = object_condition.getString("text");
String icon = object_condition.getString("icon");
String code = object_condition.getString("code");
JSONArray array_forecastday = object_forecast.getJSONArray("forecastday");
for (int i = 0; i < array_forecastday.length(); i++) {
JSONObject object_fore = array_forecastday.getJSONObject(i);
String date = object_fore.getString("date");
String date_epoch = object_fore.getString("date_epoch");
JSONObject object_day = object_fore.getJSONObject("day");
String maxtemp_c = object_day.getString("maxtemp_c");
String maxtemp_f = object_day.getString("maxtemp_f");
String mintemp_c = object_day.getString("mintemp_c");
String mintemp_f = object_day.getString("mintemp_f");
String avgtemp_c = object_day.getString("avgtemp_c");
String avgtemp_f = object_day.getString("avgtemp_f");
String maxwind_mph = object_day.getString("maxwind_mph");
String maxwind_kph = object_day.getString("maxwind_kph");
String totalprecip_mm = object_day.getString("totalprecip_mm");
String totalprecip_in = object_day.getString("totalprecip_in");
JSONObject jsonObject_condition = object_day.getJSONObject("condition");
String text_cond = jsonObject_condition.getString("text");
String icon_cond = jsonObject_condition.getString("icon");
String code_cond = jsonObject_condition.getString("code");
JSONObject object_astro = object_fore.getJSONObject("astro");
String sunrise = object_astro.getString("sunrise");
String sunset = object_astro.getString("sunset");
String moonrise = object_astro.getString("moonrise");
String moonset = object_astro.getString("moonset");
JSONArray array_hour = object_fore.getJSONArray("hour");
for (int j = 0; j < array_hour.length(); j++) {
JSONObject object_hour = array_hour.getJSONObject(j);
String time_epoch = object_hour.getString("time_epoch");
String time = object_hour.getString("time");
String temp_cc = object_hour.getString("temp_c");
String temp_ff = object_hour.getString("temp_f");
String is_day = object_hour.getString("is_day");
String wind_mphh = object_hour.getString("wind_mph");
String wind_kphh = object_hour.getString("wind_kph");
String wind_degreee = object_hour.getString("wind_degree");
String wind_dirr = object_hour.getString("wind_dir");
String pressure_mbb = object_hour.getString("pressure_mb");
String pressure_inn = object_hour.getString("pressure_in");
String precip_mmm = object_hour.getString("precip_mm");
String precip_inn = object_hour.getString("precip_in");
String humidityy = object_hour.getString("humidity");
String cloudd = object_hour.getString("cloud");
String feelslike_cc = object_hour.getString("feelslike_c");
String feelslike_ff = object_hour.getString("feelslike_f");
String windchill_c = object_hour.getString("windchill_c");
String windchill_f = object_hour.getString("windchill_f");
String heatindex_c = object_hour.getString("heatindex_c");
String heatindex_f = object_hour.getString("heatindex_f");
String dewpoint_c = object_hour.getString("dewpoint_c");
String dewpoint_f = object_hour.getString("dewpoint_f");
String will_it_rain = object_hour.getString("will_it_rain");
String will_it_snow = object_hour.getString("will_it_snow");
JSONObject jsonObject_hour = object_day.getJSONObject("condition");
String text_hour = jsonObject_hour.getString("text");
String icon_hour = jsonObject_hour.getString("icon");
String code_hour = jsonObject_hour.getString("code");
}
}
} catch (Exception e) {
e.printStackTrace();
}
希望你明白,如果遇到任何问题,请告诉我,我会帮助你。