【问题标题】:How to read yahoo weather JSON data with Jquery ajax如何使用 Jquery ajax 读取 yahoo 天气 JSON 数据
【发布时间】:2011-04-19 13:26:32
【问题描述】:

http://weather.yahooapis.com/forecastjson?w=2295424

{
"units":
{"temperature":"F","speed":"mph","distance":"mi","pressure":"in"},
"location":{"location_id":"INXX0075","city":"Madras","state_abbreviation":"*","country_abbreviation":"IN","elevation":49,"latitude":13,"longitude":80.18000000000001},
"wind":{"speed":12.00000000000000,"direction":"E"},
"atmosphere":{"humidity":"23","visibility":"4.35","pressure":"29.77","rising":"steady"},
"url":"http:\/\/weather.yahoo.com\/forecast\/INXX0075.html","logo":"http:\/\/l.yimg.com\/a\/i\/us\/nt\/ma\/ma_nws-we_1.gif","astronomy":{"sunrise":"06:20","sunset":"18:19"},"condition":{"text":"Sunny","code":"32","image":"http:\/\/l.yimg.com\/a\/i\/us\/we\/52\/32.gif","temperature":93.00000000000000},

"forecast":[{
"day":"Today","condition":"Mostly Clear","high_temperature":"91","low_temperature":"69"},{"day":"Tomorrow","condition":"Partly Cloudy","high_temperature":"90","low_temperature":"70"}
]}

我想显示“预测”

【问题讨论】:

  • 请出示您目前拥有的代码并提出一个明确的问题。 “预测”包含几个要显示的内容,“显示”的含义也不清楚。

标签: jquery ajax json


【解决方案1】:
$.ajax({
   url: "http://weather.yahooapis.com/forecastjson?w=2295424",
   dataType: "json",
   success: function(data) {
      console.log( data.forecast[0].day );
      }
 });

在 data.forecast[0].day 中,您将“day”替换为您需要的任何属性。

【讨论】:

  • 无结果 console.log(data.forecast[0].day)
  • @Srinivas Tamada >为什么使用 console.log() 只是因为我已经习惯了。这意味着您可以使用 alert() 或 $(#myElement).html(data.forecast[0].day);顺便说一句,如果你不知道“console.log”的来源,你应该在 Firefox 上尝试“Firebug”或在 Opera 中尝试“Dragonfly”。
【解决方案2】:

这应该可行:

console.debug("initYahooWeather starting");
    var urlYahooWeather = "http://weather.yahooapis.com/forecastjson?w=2295424";

    var urlFlickr = "http://weather.yahooapis.com/forecastjson?jsoncallback=?&w=2295424";
    jQuery.ajax({
        async: false,
        type: "POST",
        contentType: "application/json",
        dataType: "json",
        url: urlFlickr,
        success: function(data){
            console.dir(data);
            console.dir(data.forecast);
            // here you have to navigate the array 

        },
        error: function(msg){
            console.debug("error contacting JSON server side component...");
            console.debug(msg);
        }
    });

    console.debug("initYahooWeather stop");

我自己试过,但使用 jQuery 1.5 我收到一个解析错误:我不知道为什么,代码应该没问题。

麻子

【讨论】:

    猜你喜欢
    • 2011-02-06
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 2011-10-27
    • 2015-02-04
    • 2017-03-25
    相关资源
    最近更新 更多