如何走过json的回应?

我有这个json的反应,我试图让walke认为它可以获得诸如“湿度”和“temp_C”等天气条件。 我尝试了一些方法,但没有奏效。

({ "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://sofzh.miximages.com/jquery/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://sofzh.miximages.com/jquery/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]) } }); }; 

我认为你的主要问题是你的数据嵌套在一个名为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; }); 

如果你想将temp作为数字,你可能需要这样做:

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

要遍历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/

您的代码有两个问题。

  1. 您的变量称为数据,JSON中的第一件事是一个名为data的对象。
  2. current_condition是一个对象数组(在Javascript中,方括号[]引用数组,花括号{}引用一个对象),因此在引用temp_C之前必须说current_condition [index]。

我在这个例子json_data data重命名为json_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并单击控制台找到它。

JSON不应该包含在“()”中,除非在打开“(”)之前将其作为带有函数名称的jsonp发送。现在假设您从浏览器复制指向jsonp url的响应,并且粘贴的是复制错误。

使用$ .each使循环变得非常容易。

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