2013-02-13 4 views
1

yahoo-weatherdata를 사용하는 홈페이지에 대한 폴백을 만들려고합니다.Javascript 문자열을 복사하여 붙여 넣은 후에 JSON.parse가 작동하지 않습니다.

저는 보통 jQuery의 $ .getJSON 함수를 사용하여 json weatherdata를 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; 
    }); 

그러나 폴백 구문 분석은 작동하지 않습니다. 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}}}}}} 

누군가가 잘못된 생각을 갖고 있습니까?

감사합니다.

+0

F에 정의되지 않은 alert(mything.query.result.channel.title); allback.query.result.channel은 json 텍스트의 fallback.query.results.channel입니다. 복수 결과 ​​ –

+0

문제의 설계도를 표시 한 것이므로 코드에 몇 가지 실수가있을 수 있습니다. 그러나 이것은 문제. 문제는 구문 분석 오류입니다. – user1383029

답변

2

아포스트로피 (아무 것도 없음)와 백 슬래시를 이스케이프하는 것을 잊었습니다. '{"a":"b\"c"}'과 같은 JS 문자열이있는 경우 '{"a":"b"c"}'과 동일합니다. 이는 분명히 잘못된 JSON입니다.

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

고마워요! 이것이 해결책입니다! 그것은 지금 완벽하게 작동합니다 ... – user1383029

1

것은 당신의 문자열을 가지고, 그것으로 이것을 시도 : alert(mything.query.results.channel.title);를 대신 그냥 객체 리터럴로 순수한 JSON을 붙여 대신 이스케이프 JSON 문자열을 붙여 그 위에 JSON.parse를 호출

, 해당 문자열

SO

result = fallback.query.results.channel; 
+0

이것은 문제의 설계도를 보여 주므로 코드에 몇 가지 실수가있을 수 있지만 문제는 아닙니다. 문제는 구문 분석 오류입니다. 솔루션은 위의 한 게시물입니다. – user1383029

+0

그러나 나는 또한 귀하의 솔루션에 대한 완전한 통찰력을 갖지 못했다고 생각했습니다. 이제 왼쪽에있는 체크 표시를 클릭하여 대답을 수락해야합니다. –

관련 문제