2012-01-07 24 views
0

jsonデータを取得しようとしていますが、これを行うことはできません。私は特定の都市の気象データを取得しようとしています。ここでjsonデータを取得できませんでした

{ 
"data": 
{ 
    "current_condition": 
    [ 
     { 
      "cloudcover": "100", 
      "humidity": "100", 
      "observation_time": "01:07 PM", 
      "precipMM": "0.2", 
      "pressure": "993", 
      "temp_C": "-6", 
      "temp_F": "21", 
      "visibility": "10", 
      "weatherCode": "368", 
      "weatherDesc": 
      [ 
       { 
        "value": "Light snow showers" 
       } 
      ], 
      "weatherIconUrl": 
      [ 
       { 
        "value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0027_light_snow_showers_night.png" 
       } 
      ], 
      "winddir16Point": "N", 
      "winddirDegree": "360", 
      "windspeedKmph": "9", 
      "windspeedMiles": "6" 
     } 
    ], 
    "request": 
    [ 
     { 
      "query": "Tampere, Finland", 
      "type": "City" 
     } 
    ], 
    "weather": 
    [ 
     { 
      "date": "2012-01-07", 
      "precipMM": "2.3", 
      "tempMaxC": "-4", 
      "tempMaxF": "25", 
      "tempMinC": "-8", 
      "tempMinF": "17", 
      "weatherCode": "326", 
      "weatherDesc": 
      [ 
       { 
        "value": "Light snow" 
       } 
      ], 
      "weatherIconUrl": 
      [ 
       { 
        "value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0011_light_snow_showers.png" 
       } 
      ], 
      "winddir16Point": "NNW", 
      "winddirDegree": "336", 
      "winddirection": "NNW", 
      "windspeedKmph": "9", 
      "windspeedMiles": "5" 
     }, 
     { 
      "date": 
      "2012-01-08", 
      "precipMM": "0.0", 
      "tempMaxC": "-7", 
      "tempMaxF": "19", 
      "tempMinC": "-9", 
      "tempMinF": "17", 
      "weatherCode": "116", 
      "weatherDesc": 
      [ 
       { 
        "value": "Partly Cloudy" 
       } 
      ], 
      "weatherIconUrl": 
      [ 
       { 
        "value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png" 
       } 
      ], 
      "winddir16Point": "SSE", 
      "winddirDegree": "148", 
      "winddirection": "SSE", 
      "windspeedKmph": "5", 
      "windspeedMiles": "3" 
     } 
    ] 
} 
} 

、ここでは、私はそれがjQueryの

var container = $('.weatherContainer'); 
     var url = 'http://free.worldweatheronline.com/feed/weather.ashx?q=Tampere&format=json&num_of_days=2&key=a84523bbed133415120701&callback=?'; 
     $.getJSON(url, function(w) { 
      //console.log(w.data); 
      var contents = "<div class='c'>"; 
      $.each(w.data, function(i, res){ 
       //alert('h'); 
       $.each(res.weather, function(j,action) { 
        //alert('i'); 
        contents += "<section>" + action.tempMaxC + "</section>"; 
       }); 
      }); 
      contents += "</div>"; 
      container.append(contents); 
     }); 

ヘルプをしてください使用して取得しようとしていますどのように私のJSONデータです。どうすれば達成できますか?上記のコードは私にとってはうまくいきません。エラーが、私は天候オブジェクト内のデータを取得したい

object is undefined 
length = object.length,         jquery-latest.js (line 630) 

が何であるかをここで

。どうすれば入手できますか?ここ

[OK]を、私はenter link description here

+5

「動作しません」は問題分析ではありません。それは問題の説明でもない。 –

+0

コードを実行すると実際に何が起こりますか?コンソールにエラーが表示されますか?コメントアウトされた 'console.log(w.data)'があることに気付きました。コメントを外すとその内容はどうなりますか? – nnnnnn

+0

OKこのエラーオブジェクトは定義されていません [Break On This Error] length = object.length、 – 2619

答えて

4

あなたの問題はあなたのネストされた「各」のループではconsole.logコメントを解除した出力のイメージがあります。応答のdataオブジェクト内の各オブジェクトのjQueryに、その中の各weatherオブジェクトを見つけて繰り返し処理します。そこだけ1 weatherオブジェクトがあり、それはdataの内側に直接ですので、外側のループを削除し、この単一のループを簡素化:

$.each(w.data.weather, function(i, res) {   
    contents += "<section>" + res.tempMaxC + "</section>"; 
}); 

すべてのすべてで、あなたはかなり接近していました。マイナーチェンジではa working fiddle of your codeです

+0

ビットがあらかじめ確認されています:http://jsfiddle.net/NmukX/4/ – Krinkle

関連する問題