2013-02-06 7 views
1

멍청한 놈처럼 weather.com RSS 피드를 구문 분석하여 내 사이트에 표시하려고합니다. 아래 코드는 데이터를 성공적으로 가져 와서 경고로 표시합니다. 내 질문은 이걸 더 멀리 가져 가서 결과 피드를 표시하는 대신 구문 분석하는 방법입니다. 목표는 응답의 일부를 가져 와서 페이지의 HTML에 포함시키는 것입니다. 예를 들어, 현재 온도가있는 행이있는 테이블을 출력하려면 구문은 무엇입니까?JSON으로 RSS 데이터를 구문 분석하는 방법

<script> 
function parseRSS(url, callback) { 
    $.ajax({ 
    url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeURIComponent(url), 
    dataType: 'json', 
    success: function(data) { 
     callback(data.responseData.feed); 
    } 
    }); 
} 


parseRSS('http://rss.weather.com/weather/rss/local/USAK0012?cm_ven=LWO&cm_cat=rss&par=LWO_rss', 
function(json) 
{ 
    alert("Result: " + JSON.stringify(json)); 
}); 
</script> 

답변

1

누구에게 도움이된다면, 어떻게 끝내려고합니까? weather.com 대신에 accuweather.com의 피드와 함께갔습니다.

1) 필자가 필요로하는 현재 날씨 값을 포함하는 피드 "title"필드의 값을 얻었습니다.

var weatherFeed = json; 
var currentConditions = weatherFeed.entries[0].title; 

2) 예를 들어 currentTemperature에 대한 특정 요소를 추출 :

var firstColonIndex = currentConditions.indexOf(":"); 
var secondColonIndex = currentConditions.indexOf(":", firstColonIndex + 1); 
var currentTemperature = currentConditions.substring(secondColonIndex + 1); 

3) jQuery를 사용하여 HTML을 내장하고 추출 된 기상 값을 포함을, 다음에 div 요소 내에서 모든 것을 넣어 내 페이지 :

<div class="tile-content" id="MyCityWeather"> 
    <script type="text/javascript"> 
    var weatherElement = "#MyCityWeather"; 
    var temperatureElement = $("<h2/>", 
    { 
     style: "margin-left: 25px!important; font-size: 46px!important;" 
    }); 
    temperatureElement.append(currentTemperature); 
    temperatureElement.appendTo(weatherElement); 
    </script> 
</div> 
관련 문제