1

初心者として、weather.com の RSS フィードを解析して自分のサイトに表示しようとしています。以下のコードは、データを正常に取得し、アラートとして表示します。私の質問は、これをさらに進めて、結果のフィードを単に表示するのではなく解析する方法ですか? 目標は、応答の一部を取得し、それをページの HTML に埋め込むことです。たとえば、現在の温度を含む行を含むテーブルを出力したい場合、どのような構文になりますか?

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

1 に答える 1

1

それが誰かを助けるなら、これが私がそれをやった方法です。私は、weather.com の代わりに accuweather.com からのフィードを使用しました。

1)フィードの「タイトル」フィールドの値を取得しました。これには、必要な現在の天気の値が含まれていました。

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>
于 2013-02-08T22:39:54.677 に答える