作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个 json 响应,我试图通过它来获取天气条件,例如“湿度”和“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://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])
}
});
};
最佳答案
我认为您的主要问题是您的数据嵌套在名为 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);
});
关于jquery - 如何遍历 json 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9449638/
我是一名优秀的程序员,十分优秀!