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&format=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

アポストロフィ (何もありません) とバックスラッシュをエスケープするのを忘れました。次のような JS 文字列がある場合、'{"a":"b\"c"}'これ'{"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 に答える