2

私は実際にAPIを使用して、JSONデータを取得し、実際のHTMLファイルに投入して、結果をきれいに見せることにまったく慣れていません。

HTML/CSS/jQuery が得意です。しかし、これはjQueryの詳細ではないようです(私は基本を行うことができます)

これは、Weather Underground の API から取得した JSON データの例です。

    "current_observation": {
    "image": {
    "url":"http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png",
    "title":"Weather Underground",
    "link":"http://www.wunderground.com"
    },
    "display_location": {
    "full":"Bowling Green, KY",
    "city":"Bowling Green",
    "state":"KY",
    "state_name":"Kentucky",
    "country":"US",
    "country_iso3166":"US",
    "zip":"42101",
    "latitude":"37.02899933",
    "longitude":"-86.46366119",
    "elevation":"154.00000000"
    }

display_locationの中にあることがわかりますcurrent_observation

自分のウェブサイトでh1として引き出して表示fullしたい(実際にはもっと情報を提供したいのですが、これを書き留めた後は残りを処理できるように感じます. )

だからここに私が現在持っているものがあります:

<!DOCTYPE HTML>
<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    </head>
    <body>
        <section id="area"></section>
    </body>
    <script type="text/javascript">
        $().ready(function(){ 
            $.getJSON("http://api.wunderground.com/api/[MY API KEY]/conditions/q/autoip.json",
            function(data){
                $.each(data){
                    content = '<h1>' + json.current_observation.display_location.full + '</h1>';
                    $(content).appendTo("#area");
                 });
              });
           });
    </script>
</html>

それは動作しません :-/

どんな助けも素晴らしいです。

4

2 に答える 2

1

これを見てください:

content = '<h1>' + json.current_observation.display_location.full + '</h1>';

とはどういうjson意味ですか?? また$.each(data){、無効な構文です。ドキュメントを参照してください。あなたのeachループはおそらく次のようになります

$.each(data, function(i, json) {
    content = '<h1>' + json.current_observation.display_location.full + '</h1>';
    $(content).appendTo("#area");
});

data返されたものが質問に含まれているようなオブジェクトの配列である場合に機能します。あなたはその形を見るべきです..あなたはループconsole.log(data)を排除してただ使うことができるかもしれませんeach

content = '<h1>' + data.current_observation.display_location.full + '</h1>';
于 2012-05-24T16:34:06.080 に答える
-1

無駄な括弧のようです。試す :

$().ready(function(){ 
        $.getJSON("http://api.wunderground.com/api/[MY API KEY]/conditions/q/autoip.json",
        function(data){
            $.each(data){
                content = '<h1>' + json.current_observation.display_location.full + '</h1>';
                $(content).appendTo("#area");
             } // Change here
          });
       });
于 2012-05-24T16:29:03.713 に答える