0

json 値をスクリプトにバインドし、html での出力が必要な方法。天気のデータと画像を取得しようとしていますが、解決策はありません。助けて

ここにjqueryのサンプルがあります

$(function(){
   $.getJSON("http://query.yahooapis.com/v1/public/yql", {
      q: "select * from json where url=\"http://api.wunderground.com/api/91bbc8aab3ab1f34/geolookup/conditions/q/IN/Chennai.json\"",
      format: "json"
}, function(data) {
   var $content = $("#content")
   if (data.query.results) {
     $content.text(JSON.stringify(data.query.results));
   } else {
      $content.text('no such code: ' + code);
   }

 });
});​

html のコード

<script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>

コンテンツは div で、アイコンはイメージ タグです

<div id="content">
<img id="weather_icon" />

アプリケーションを実行すると、データだけが取得されます。API からの天気画像とデータが必要です。助けてください

4

2 に答える 2

0

uが提供したフィドルによると..

に都市名を表示するために full を使用しました<p id="temp"></p>

$('#temp').html(data.query.results.json.current_observation.display_location.full); 

ここにフィドルがあります

http://jsfiddle.net/SH7Ku/1/

更新しました

ここに更新されたフィドルがあります....温度で

http://jsfiddle.net/SH7Ku/2/

同様に、あなたは休むことができます...

于 2012-12-12T12:13:13.277 に答える
0

やってみると、お天気アイコンが表示できました。 imageタグにURLをセットして画像を表示するようにjQueryを修正しました。コンテンツ div に表示したいデータを掘り下げるだけです。

        $(function(){
        $.getJSON("http://query.yahooapis.com/v1/public/yql", {
            q: "select * from json where url=\"http://api.wunderground.com/api/91bbc8aab3ab1f34/geolookup/conditions/q/IN/Chennai.json\"",
            format: "json"
            }, 
            function(data) {
                // Dig for the data from the JSON object. 
                //  The image URL can be found at: query > results > json > current_location > icon_url
                $('#weather_icon').attr('src', data.query.results.json.current_observation.icon_url);
                //  Do the same for any data you want displayed for the content to be displayed..
            });
    });
于 2012-12-12T08:33:42.930 に答える