1

我尝试为使用 yahoo-weatherdata 的主页创建后备。

我通常使用 jQuery 的 $.getJSON 函数将 yahoo 天气数据作为 json 获取。这工作正常。我正在使用以下代码:

var url = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D2502265&format=json'

$.getJSON(url, function(r){

        result = r.query.results.channel;

        //Do things

    }).error(function(){
        //Fehler beim HTTP-Request
        debugger;

        //Fallback
        var fallback = JSON.parse('{"query":{"count":1,"created":"2013-02-12T23:58:55Z","lang":"en-US","results":{"channel":{"title":"Yahoo! Weather - Sunnyvale, CA","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html","description":"Yahoo! Weather for Sunnyvale, CA","language":"en-us","lastBuildDate":"Tue, 12 Feb 2013 2:56 pm PST","ttl":"60","location":{"city":"Sunnyvale","country":"United States","region":"CA"},"units":{"distance":"mi","pressure":"in","speed":"mph","temperature":"F"},"wind":{"chill":"60","direction":"320","speed":"8"},"atmosphere":{"humidity":"39","pressure":"30.32","rising":"2","visibility":"10"},"astronomy":{"sunrise":"7:01 am","sunset":"5:43 pm"},"image":{"title":"Yahoo! Weather","width":"142","height":"18","link":"http://weather.yahoo.com","url":"http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"},"item":{"title":"Conditions for Sunnyvale, CA at 2:56 pm PST","lat":"37.37","long":"-122.04","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html","pubDate":"Tue, 12 Feb 2013 2:56 pm PST","condition":{"code":"34","date":"Tue, 12 Feb 2013 2:56 pm PST","temp":"60","text":"Fair"},"description":"\n<img src=\"http://l.yimg.com/a/i/us/we/52/34.gif\"/><br />\n<b>Current Conditions:</b><br />\nFair, 60 F<BR />\n<BR /><b>Forecast:</b><BR />\nTue - Clear. High: 60 Low: 40<br />\nWed - Sunny. High: 64 Low: 42<br />\n<br />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html\">Full Forecast at Yahoo! Weather</a><BR/><BR/>\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)<br/>\n","forecast":[{"code":"31","date":"12 Feb 2013","day":"Tue","high":"60","low":"40","text":"Clear"},{"code":"32","date":"13 Feb 2013","day":"Wed","high":"64","low":"42","text":"Sunny"}],"guid":{"isPermaLink":"false","content":"USCA1116_2013_02_13_7_00_PST"}}}}}}');
        result = fallback.query.result.channel;
    }); 

但是后备解析不起作用。

我得到错误:

SyntaxError:意外的令牌(Chrome)

我尝试解析的字符串只是以下内容的复制和粘贴:http: //query.yahooapis.com/v1/public/yql ?q=select%20 *%20from%20weather.forecast%20where%20woeid% 3D2502265&格式=json

JSONLint 告诉我,一切正常:

http://jsonlint.com/?json={%22query%22:{%22count%22:1,%22created%22:%222013-02-13T00:12:44Z%22,%22lang%22:%22en-US%22,%22results%22:{%22channel%22:{%22title%22:%22Yahoo!%20Weather%20-%20Sunnyvale,%20CA%22,%22link%22:%22http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html%22,%22description%22:%22Yahoo!%20Weather%20for%20Sunnyvale,%20CA%22,%22language%22:%22en-us%22,%22lastBuildDate%22:%22Tue,%2012%20Feb%202013%202:56%20pm%20PST%22,%22ttl%22:%2260%22,%22location%22:{%22city%22:%22Sunnyvale%22,%22country%22:%22United%20States%22,%22region%22:%22CA%22},%22units%22:{%22distance%22:%22mi%22,%22pressure%22:%22in%22,%22speed%22:%22mph%22,%22temperature%22:%22F%22},%22wind%22:{%22chill%22:%2260%22,%22direction%22:%22320%22,%22speed%22:%228%22},%22atmosphere%22:{%22humidity%22:%2239%22,%22pressure%22:%2230.32%22,%22rising%22:%222%22,%22visibility%22:%2210%22},%22astronomy%22:{%22sunrise%22:%227:01%20am%22,%22sunset%22:%225:43%20pm%22},%22image%22:{%22title%22:%22Yahoo!%20Weather%22,%22width%22:%22142%22,%22height%22:%2218%22,%22link%22:%22http://weather.yahoo.com%22,%22url%22:%22http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif%22},%22item%22:{%22title%22:%22Conditions%20for%20Sunnyvale,%20CA%20at%202:56%20pm%20PST%22,%22lat%22:%2237.37%22,%22long%22:%22-122.04%22,%22link%22:%22http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html%22,%22pubDate%22:%22Tue,%2012%20Feb%202013%202:56%20pm%20PST%22,%22condition%22:{%22code%22:%2234%22,%22date%22:%22Tue,%2012%20Feb%202013%202:56%20pm%20PST%22,%22temp%22:%2260%22,%22text%22:%22Fair%22},%22description%22:%22\n%3Cimg%20src=\%22http://l.yimg.com/a/i/us/we/52/34.gif\%22/%3E%3Cbr%20/%3E\n%3Cb%3ECurrent%20Conditions:%3C/b%3E%3Cbr%20/%3E\nFair,%2060%20F%3CBR%20/%3E\n%3CBR%20/%3E%3Cb%3EForecast:%3C/b%3E%3CBR%20/%3E\nTue%20-%20Clear.%20High:%2060%20Low:%2040%3Cbr%20/%3E\nWed%20-%20Sunny.%20High:%2064%20Low:%2042%3Cbr%20/%3E\n%3Cbr%20/%3E\n%3Ca%20href=\%22http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html\%22%3EFull%20Forecast%20at%20Yahoo!%20Weather%3C/a%3E%3CBR/%3E%3CBR/%3E\n(provided%20by%20%3Ca%20href=\%22http://www.weather.com\%22%20%3EThe%20Weather%20Channel%3C/a%3E)%3Cbr/%3E\n%22,%22forecast%22:[{%22code%22:%2231%22,%22date%22:%2212%20Feb%202013%22,%22day%22:%22Tue%22,%22high%22:%2260%22,%22low%22:%2240%22,%22text%22:%22Clear%22},{%22code%22:%2232%22,%22date%22:%2213%20Feb%202013%22,%22day%22:%22Wed%22,%22high%22:%2264%22,%22low%22:%2242%22,%22text%22:%22Sunny%22}],%22guid%22:{%22isPermaLink%22:%22false%22,%22content%22:%22USCA1116_2013_02_13_7_00_PST%22}}}}}}

有人知道出了什么问题吗?

谢谢!

4

2 回答 2

2

您忘记转义撇号(没有)和反斜杠。如果你有一个像'{"a":"b\"c"}'它一样的 JS 字符串'{"a":"b"c"}'- 这显然是无效的 JSON。

无需粘贴未转义的 JSON字符串并调用JSON.parse它,只需将纯 JSON 粘贴为对象文字:

var fallback = {"query":{"count":1,"created":…},…};
于 2013-02-13T00:21:45.587 回答
1

拿你的字符串,用它试试这个: alert(mything.query.results.channel.title);而不是alert(mything.query.result.channel.title);那个字符串中没有定义的

所以

 result = fallback.query.results.channel;
于 2013-02-13T00:32:13.577 回答