2012-02-26 64 views
2

我有這樣的JSON響應,我想walke認爲它得到了天氣條件,如「溼度」和「temp_C」等。我嘗試了一些方法,但沒有奏效。如何通過json響應?

({ "data" : { "current_condition" : [ { "cloudcover" : "50", 
      "humidity" : "44", 
      "observation_time" : "12:10 AM", 
      "precipMM" : "0.0", 
      "pressure" : "1013", 
      "temp_C" : "-2", 
      "temp_F" : "29", 
      "visibility" : "16", 
      "weatherCode" : "116", 
      "weatherDesc" : [ { "value" : "Partly Cloudy" } ], 
      "weatherIconUrl" : [ { "value" : "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png" } ], 
      "winddir16Point" : "W", 
      "winddirDegree" : "280", 
      "windspeedKmph" : "24", 
      "windspeedMiles" : "15" 
      } ], 
     "request" : [ { "query" : "Rochester, United States Of America", 
      "type" : "City" 
      } ], 
     "weather" : [ { "date" : "2012-02-25", 
      "precipMM" : "2.2", 
      "tempMaxC" : "-1", 
      "tempMaxF" : "31", 
      "tempMinC" : "-5", 
      "tempMinF" : "24", 
      "weatherCode" : "116", 
      "weatherDesc" : [ { "value" : "Partly Cloudy" } ], 
      "weatherIconUrl" : [ { "value" : "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png" } ], 
      "winddir16Point" : "W", 
      "winddirDegree" : "281", 
      "winddirection" : "W", 
      "windspeedKmph" : "54", 
      "windspeedMiles" : "34" 
      } ] 
    } }) 

我嘗試這些:

$.getJSON(urlFromMyAPI, function (data) { 
    alert(data.current_condition.temp_C); 
    alert(data.temp_C); 
    alert(data[current_condition].temp_C); 
    // I also use loop 
    for (i = 0; i <= 3; i++) { 
     alert(data.current_condition[i]) 
    } 
}); 
}; 
+0

是,在你的數據的數據,或者你是怎麼貼? – Sinetheta 2012-02-26 01:14:19

+0

不知道我收到了你的問題,但這個數據是什麼來自世界天氣在線API回來。 – Timmy 2012-02-26 01:19:00

回答

3

我認爲你的主要問題是,你的數據是嵌套在名爲data所以你需要參考額外的水平進去一個對象內。這也是一個更容易看到當你格式化你這樣的反應,所以你可以看到嵌套對象和數組更清楚你有什麼:

({ "data": { 
    "current_condition": [ 
     { 
      "cloudcover": "50", 
      "humidity": "44", 
      "observation_time": "12:10 AM", 
      "precipMM": "0.0", 
      "pressure": "1013", 
      "temp_C": "-2", 
      "temp_F": "29", 
      "visibility": "16", 
      "weatherCode": "116", 
      "weatherDesc": [ 
       {"value": "Partly Cloudy" } 
      ], 
      "weatherIconUrl": [ 
       {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0004_black_low_cloud.png" } 
      ], 
      "winddir16Point": "W", 
      "winddirDegree": "280", 
      "windspeedKmph": "24", 
      "windspeedMiles": "15" 
     } 
    ], 
    "request": [ 
     {"query": "Rochester, United States Of America", "type": "City" } 
    ], 
    "weather": [ 
     { 
      "date": "2012-02-25", 
      "precipMM": "2.2", 
      "tempMaxC": "-1", 
      "tempMaxF": "31", 
      "tempMinC": "-5", 
      "tempMinF": "24", 
      "weatherCode": "116", 
      "weatherDesc": [ 
       {"value": "Partly Cloudy" } 
      ], 
      "weatherIconUrl": [ 
       {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png" } 
      ], 
      "winddir16Point": "W", 
      "winddirDegree": "281", 
      "winddirection": "W", 
      "windspeedKmph": "54", 
      "windspeedMiles": "34" 
     } 
    ] 
} 
}) 

這就是說,如果你想獲得當前的狀態temp_C ,它會是這樣(注意我對你的匿名函數改變參數的名稱,使代碼容易混淆):

$.getJSON(urlFromMyAPI, function(response){ 
    var temp = response.data.current_condition[0].temp_C; 
}); 

如果你想臨時爲數字,你可能需要這樣做:

$.getJSON(urlFromMyAPI, function(response){ 
    var temp = parseInt(response.data.current_condition[0].temp_C, 10); 
}); 
+0

它的工作夥伴。不能給你+1我沒有信譽:) – Timmy 2012-02-26 01:31:12

+0

@ user1233225 - 適當的時間量過去之後,你可以按旁打勾你最喜歡的答案將其標記爲最佳答案,你也將獲得一些聲望點數爲了那個原因。 – jfriend00 2012-02-26 01:37:11

1

要遍歷包含JSON對象,你需要訪問data.data.current_condition內部數組:

for(i = 0; i <= 3; i++){ 
    alert(data.data.current_condition[i]); 

    var properties = data.data.current_condition[i]; 

    for(var y in properties) 
     alert(properties[y]); 
} 

http://jsfiddle.net/m7TZx/

+0

它給了我「未定義」 – Timmy 2012-02-26 01:20:07

+0

@ user1233225剛剛更新的答案... – xandercoded 2012-02-26 01:28:54

+0

http://jsfiddle.net/m7TZx/ – xandercoded 2012-02-26 01:36:12

0

有兩個問題與您的代碼。

  1. 您的變量被稱爲數據,而JSON中的第一件事是一個名爲data的對象。
  2. current_condition是物體的陣列(在Javascript,方括號[]引用數組,大括號{}指一個對象),所以必須參照temp_C之前說current_condition [指數]。

我在這個例子中改名datajson_data以避免混淆:

$.getJSON(urlFromMyAPI, function(json_data){ 
    console.log(json_data.data.current_condition[0].temp_C); 
}); 

如果您有多個current_condition對象,你可以使用一個for循環通過他們去:

$.getJSON(urlFromMyAPI, function(json_data){ 
    var current_conditions = json_data.data.current_condition; 
    for(var i=0; i < current_conditions.length; i++) { 
     console.log(current_conditions.temp_C); 
    } 
}); 

你如果您想以更好的格式查看它,可以使用Javascript修飾符(例如http://jsbeautifier.org/)。

您可能會發現console.logalert更加有用。大多數瀏覽器都有一個控制檯,在谷歌瀏覽器中,您可以按F12並單擊控制檯找到它。

0

的JSON不應被包裹在「()」時,它是打開「(」前發送作爲JSONP與函數名除外。現在將假設你複製了來自指向jsonp url的瀏覽器的響應,並且粘貼的是複製錯誤。

使用$。每次使循環很容易。

$.each(data.data.current_condition[0], function (key, value){ 
    console.log('Key:', key, ' Value:', value) 
})