【问题标题】:Mistake reading json错误读取json
【发布时间】:2017-01-05 11:37:32
【问题描述】:

我正在尝试从 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

【问题讨论】:

  • 考虑使用gson library....它会让你的生活更轻松!
  • @Androi: `Log.d("Response: ", "> " + jsonStr);` 行中有什么数据?
  • 您想从中获得什么 - 位置、预测或当前?
  • 调试它。添加日志。尝试一个接一个地记录跟踪 json 对象。它有助于理解你的错误
  • 您可以使用在线 Json 解析器,例如 this,这将有助于您理解 Json 对象。只需复制粘贴 GET 结果并点击查看器选项卡。另外,如前所述,我建议您使用其中一个优秀的库。

标签: android json android-asynctask


【解决方案1】:

我认为您的问题是 hourforecastday 中的数组,而不是您当前处理的 forecast 中的数组。

希望对你有帮助!

【讨论】:

  • 也许是这样,我怎样才能在另一个阵列中发生冲突?谢谢
  • 使用porhoraca = eventos.getJSONArray("hour");
【解决方案2】:

最后我实现了你的代码,调试它并在 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();
                    }

【讨论】:

    【解决方案3】:

    这里是您问题的完整解决方案,您只需在 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();
                }
    

    希望您理解,如果遇到任何问题,请告诉我,我会帮助您。

    【讨论】:

      猜你喜欢
      • 2014-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 2018-04-01
      • 2018-12-14
      • 1970-01-01
      相关资源
      最近更新 更多