2012-07-03 35 views
2

我很難理解如何讀取包含「@attributes」的JSON對象。JSON @attributes

的Javascript

$.ajax({ 
    type: "GET", 
    dataType: 'json', 
    url: "http://..../script/weather.php?r="+req, 
    success: function(data){ 
     alert(data.weather.forecast_information.city[0].data) 
    } 
}); 

JSON響應:

{ 
    "@attributes": { 
     "version": "1" 
    }, 
    "weather": { 
     "@attributes": { 
      "module_id": "0", 
      "tab_id": "0", 
      "mobile_row": "0", 
      "mobile_zipped": "1", 
      "row": "0", 
      "section": "0" 
     }, 
     "forecast_information": { 
      "city": { 
       "@attributes": { 
        "data": "Kreuzlingen, Thurgovia" 
       } 
      }, 
      "postal_code": { 
       "@attributes": { 
        "data": "kreuzlingen" 
       } 
      }, 
      "latitude_e6": { 
       "@attributes": { 
        "data": "" 
       } 
      }, 
      "longitude_e6": { 
       "@attributes": { 
        "data": "" 
       } 
      }, 
      "forecast_date": { 
       "@attributes": { 
        "data": "2012-07-03" 
       } 
      }, 
      "current_date_time": { 
       "@attributes": { 
        "data": "1970-01-01 00:00:00 +0000" 
       } 
      }, 
      "unit_system": { 
       "@attributes": { 
        "data": "US" 
       } 
      } 
     }, 
     "current_conditions": { 
      "condition": { 
       "@attributes": { 
        "data": "Cloudy" 
       } 
      }, 
      "temp_f": { 
       "@attributes": { 
        "data": "70" 
       } 
      }, 
      "temp_c": { 
       "@attributes": { 
        "data": "21" 
       } 
      }, 
      "humidity": { 
       "@attributes": { 
        "data": "Humidity: 68%" 
       } 
      }, 
      "icon": { 
       "@attributes": { 
        "data": "/ig/images/weather/cloudy.gif" 
       } 
      }, 
      "wind_condition": { 
       "@attributes": { 
        "data": "Wind: N at 0 mph" 
       } 
      } 
     }, 
     "forecast_conditions": [ 
      { 
       "day_of_week": { 
        "@attributes": { 
         "data": "Tue" 
        } 
       }, 
       "low": { 
        "@attributes": { 
         "data": "55" 
        } 
       }, 
       "high": { 
        "@attributes": { 
         "data": "72" 
        } 
       }, 
       "icon": { 
        "@attributes": { 
         "data": "/ig/images/weather/thunderstorm.gif" 
        } 
       }, 
       "condition": { 
        "@attributes": { 
         "data": "Thunderstorm" 
        } 
       } 
      }, 
      { 
       "day_of_week": { 
        "@attributes": { 
         "data": "Wed" 
        } 
       }, 
       "low": { 
        "@attributes": { 
         "data": "66" 
        } 
       }, 
       "high": { 
        "@attributes": { 
         "data": "79" 
        } 
       }, 
       "icon": { 
        "@attributes": { 
         "data": "/ig/images/weather/chance_of_storm.gif" 
        } 
       }, 
       "condition": { 
        "@attributes": { 
         "data": "Chance of Storm" 
        } 
       } 
      }, 
      { 
       "day_of_week": { 
        "@attributes": { 
         "data": "Thu" 
        } 
       }, 
       "low": { 
        "@attributes": { 
         "data": "61" 
        } 
       }, 
       "high": { 
        "@attributes": { 
         "data": "77" 
        } 
       }, 
       "icon": { 
        "@attributes": { 
         "data": "/ig/images/weather/chance_of_storm.gif" 
        } 
       }, 
       "condition": { 
        "@attributes": { 
         "data": "Chance of Storm" 
        } 
       } 
      }, 
      { 
       "day_of_week": { 
        "@attributes": { 
         "data": "Fri" 
        } 
       }, 
       "low": { 
        "@attributes": { 
         "data": "63" 
        } 
       }, 
       "high": { 
        "@attributes": { 
         "data": "79" 
        } 
       }, 
       "icon": { 
        "@attributes": { 
         "data": "/ig/images/weather/chance_of_rain.gif" 
        } 
       }, 
       "condition": { 
        "@attributes": { 
         "data": "Chance of Rain" 
        } 
       } 
      } 
     ] 
    } 
} 

ERROR(鉻);

遺漏的類型錯誤:無法讀取屬性未定義

問題的「數據」的同時,如何讓「魯林根,Thurgovia」的警示?

+0

'在上面的JSON @ attributes'沒有任何特殊的鍵。 其開始時用'@'表示javascript的簡單字符串。因此,直接使用@engineer解釋它與一個簡單的答案與自我解釋 – swapnilsarwe

回答

9

獲取這樣的:

alert(data.weather.forecast_information.city["@attributes"].data) 
+1

foo ['bar'] === foo.bar – insign

相關問題