2012-02-26 128 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) 
})