2012-01-07 288 views
0

我想获取json数据,但我无法这样做。我正在尝试获取特定城市的天气数据。这里是我的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); 
     }); 

请帮助。我怎样才能实现它?上面的代码不适合我。

这里的错误是什么

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

我想天气对象中的数据。我怎么弄到的?

确定这里是输出的图像时,我去掉CONSOLE.LOG enter link description here

+5

“不起作用”不是问题分析。这甚至不是问题描述。 – 2012-01-07 15:16:52

+0

运行代码时实际发生了什么?你在控制台中遇到错误吗?我注意到你已经注释掉了'console.log(w.data)' - 如果取消注释,那么打印的内容是什么? – nnnnnn 2012-01-07 15:19:33

+0

确定它给这个错误对象是未定义的 [Break On This Error] length = object.length, – 2619 2012-01-07 15:19:48

回答

4

你的问题是你嵌套“每个”循环。您在响应的data对象中告诉jQuery每个对象,找到这些对象中的每个对象并遍历它。只有一个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 2012-01-10 23:23:53