2013-08-07 35 views
0

我想显示天气预报。到目前为止,我只设法显示一天。Openweathermaps解析json jquery预测

HTML & Jquery的代码:http://pastebin.com/cpZ0pE7G

的JSON是显示在此格式。

{ 
    "cod":"200", 
    "message":0.0152, 
    "city":{ 
     "id":524901, 
     "name":"Moscow", 
     "coord":{ 
     "lon":37.615555, 
     "lat":55.75222 
     }, 
     "country":"RU", 
     "population":1000000 
    }, 
    "cnt":4, 
    "list":[ 
     { 
     "dt":1375866000, 
     "temp":{ 
      "day":26.19, 
      "min":21.82, 
      "max":26.19, 
      "night":21.82, 
      "eve":25.22, 
      "morn":26.19 
     }, 
     "pressure":1010.53, 
     "humidity":77, 
     "weather":[ 
      { 
       "id":801, 
       "main":"Clouds", 
       "description":"few clouds", 
       "icon":"02d" 
      } 
     ], 
     "speed":1.6, 
     "deg":4, 
     "clouds":24 
     }, 
     { 
     "dt":1375952400, 
     "temp":{ 
      "day":24.92, 
      "min":17.73, 
      "max":25.32, 
      "night":17.73, 
      "eve":24.71, 
      "morn":17.75 
     }, 
     "pressure":1013.91, 
     "humidity":71, 
     "weather":[ 
      { 
       "id":800, 
       "main":"Clear", 
       "description":"sky is clear", 
       "icon":"01d" 
      } 
     ], 
     "speed":1.61, 
     "deg":20, 
     "clouds":0 
     }, 
     { 
     "dt":1376038800, 
     "temp":{ 
      "day":25.87, 
      "min":16.74, 
      "max":26.65, 
      "night":22.17, 
      "eve":26.42, 
      "morn":16.74 
     }, 
     "pressure":1011.31, 
     "humidity":71, 
     "weather":[ 
      { 
       "id":800, 
       "main":"Clear", 
       "description":"sky is clear", 
       "icon":"01d" 
      } 
     ], 
     "speed":1.66, 
     "deg":252, 
     "clouds":0 
     }, 
     { 
     "dt":1376125200, 
     "temp":{ 
      "day":22.72, 
      "min":19.68, 
      "max":25.42, 
      "night":19.68, 
      "eve":24.82, 
      "morn":21.02 
     }, 
     "pressure":1007.87, 
     "humidity":73, 
     "weather":[ 
      { 
       "id":502, 
       "main":"Rain", 
       "description":"heavy intensity rain", 
       "icon":"10d" 
      } 
     ], 
     "speed":2.11, 
     "deg":278, 
     "clouds":80, 
     "rain":15 
     } 
    ] 
} 

我只能解析第一行。我如何解析其他线?

+0

请在此处包含代码的相关部分,而不是在pastebin上的代码转储旁边。 –

+0

我设法解析压力和湿度:'weather.list.humidity' –

+0

但是当我尝试temp:'weather.list.temp.day'我得到一个错误:'未捕获TypeError:无法读取属性'天'未定义' –

回答

0

当您看到方括号时,它是一个数组,因此您必须通过数组键找到该变量。 weather.list[0].temp.day会返回你26.19。 weather.list[0].weather[0].main将返回你“云”和weather.list[1].weather[0].main将返回你“清除”。