【问题标题】:Parsing JSONP from Wunderground with jQuery使用 jQuery 从 Wunderground 解析 JSONP
【发布时间】:2013-05-20 05:10:47
【问题描述】:

我在使用 JavaScript 和 Ajax 解析“wunderground”API 时遇到问题。我可以得到一些值,而有些则不能。这是我使用的 API 的链接:

{
   "forecast":{
      "simpleforecast":{
         "forecastday":[
            {
               "date":{
                  "epoch":"1369429200",
                  "pretty":"11:00 PM CEST on May 24, 2013",
                  "day":24,
                  "month":5,
                  "year":2013,
                  "yday":143,
                  "hour":23,
                  "min":"00",
                  "sec":0,
                  "isdst":"1",
                  "monthname":"May",
                  "weekday_short":"Fri",
                  "weekday":"Friday",
                  "ampm":"PM",
                  "tz_short":"CEST",
                  "tz_long":"Europe/Ljubljana"
               },
               "period":1,
               "high":{
                  "fahrenheit":"63",
                  "celsius":"17"
               },
               "low":{
                  "fahrenheit":"45",
                  "celsius":"7"
               },
               "conditions":"Rain",
               "icon":"rain",
               "icon_url":"http://icons-ak.wxug.com/i/c/k/rain.gif",
               "skyicon":"cloudy",
               "pop":90,
               "qpf_allday":{
                  "in":0.47,
                  "mm":11.9
               },
               "qpf_day":{
                  "in":0.38,
                  "mm":9.7
               },
               "qpf_night":{
                  "in":0.05,
                  "mm":1.3
               },
               "snow_allday":{
                  "in":0,
                  "cm":0
               },
               "snow_day":{
                  "in":0,
                  "cm":0
               },
               "snow_night":{
                  "in":0,
                  "cm":0
               },
               "maxwind":{
                  "mph":4,
                  "kph":6,
                  "dir":"West",
                  "degrees":270
               },
               "avewind":{
                  "mph":2,
                  "kph":3,
                  "dir":"SSW",
                  "degrees":204
               },
               "avehumidity":62,
               "maxhumidity":72,
               "minhumidity":57
            }
         ]
      }
   }
}

我想为摄氏度解析值 17。到目前为止,我有这个:

jQuery(document).ready(function($) {
  $.ajax({
  url :     "http://api.wunderground.com/api/XXXXXXXXXX/geolookup/conditions/forecast/q/Slovenia/Maribor.json",
  dataType : "jsonp",
  success : function(parsed_json) {
  var forecast_1 = parsed_json['forecast']['simpleforecast']['forecastday']['celsius'];
  document.getElementById("forecast_1").innerHTML = forecast_1;
  }
  });
});
</script>

我得到了“未定义”的值。请帮忙? 谢谢

【问题讨论】:

    标签: jquery ajax json parsing jsonp


    【解决方案1】:

    属性day是一个对象数组,所以你必须先选择数组索引。此外,它没有直接属性celsius。但是,highlow 下有两个 celsius 属性。所以你必须决定,要解决哪个问题。

    总而言之,结果可能如下所示:

    var forecast_1 = parsed_json['forecast']['simpleforecast']['forecastday'][0]['high']['celsius'];
    

    此外,正确的输出格式有助于理解它。使用,例如,http://jsonformatter.curiousconcept.com/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 2012-03-16
      • 2023-03-22
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      • 2013-07-17
      相关资源
      最近更新 更多