Question

I try to create a fallback for a homepage which uses yahoo-weatherdata.

I normally get the yahoo weatherdata as json with the $.getJSON function of jQuery. This works fine. I`m using the following code:

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;
    }); 

But the fallback parsing does not work.

I get the error:

SyntaxError: Unexpected token (Chrome)

The string I try to parse is just a copy and paste of the content of: http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D2502265&format=json

JSONLint tells me, that everything is ok:

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}}}}}}

Does somebody have an idea what is going wrong?

Thank you!

Was it helpful?

Solution

You forgot to escape apostrophes (there are none) and backslashes. If you have a JS string like '{"a":"b\"c"}' it's the same as '{"a":"b"c"}' - which is obviously invalid JSON.

Instead of pasting an unescaped JSON string and calling JSON.parse on it, just paste the pure JSON as an object literal:

var fallback = {"query":{"count":1,"created":…},…};

OTHER TIPS

take your string, and try this with it: alert(mything.query.results.channel.title); instead of the alert(mything.query.result.channel.title); which is not defined in that string

SO

 result = fallback.query.results.channel;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top