gpt4 book ai didi

jquery - 如何遍历 json 响应?

转载 作者:行者123 更新时间:2023-12-01 03:01:01 25 4
gpt4 key购买 nike

我有这个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com