2012-01-07 12 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) 

무엇인지 여기

. 어떻게해야합니까? 내가 enter link description here

+5

"작동하지 않음"은 문제 분석이 아닙니다. 그것은 심지어 문제 설명이 아닙니다. –

+0

코드를 실행하면 실제로 어떻게됩니까? 콘솔에서 오류가 발생합니까? 나는 주석 처리 된'console.log (w.data)'가 있다는 것을 알아 차렸는데, 주석을 제거하면 그 내용은 무엇이겠습니까? – nnnnnn

+0

ok이 오류는 객체가 정의되지 않았 음을 나타냅니다. console.log (w.data)의 주석을 제거 할 때 [Break On This Error] 길이 = object.length, – 2619

답변

4

귀하에 문제가 중첩 된 "각"루프입니다 CONSOLE.LOG의 주석을 해제 할 때 여기에 좋아

출력의 이미지입니다. 응답의 data 오브젝트에있는 각 오브젝트에 대해 jQuery에 알려주고 그 안에있는 weather 오브젝트를 찾아서 반복하십시오. 이 하나의 weather 객체이며 data의 내부에 직접, 그래서이 단일 루프와 외부 루프를 제거하고 단순화 :

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

모두 모두

, 당신은 아주 가까이 있었다. 사소한 변경 사항이있는 a working fiddle of your code입니다.

+0

조금 prettified : http://jsfiddle.net/NmukX/4/ – Krinkle

관련 문제