gpt4 book ai didi

jquery - 使用jquery读取json文件

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

我正在尝试使用 jquery 中的 getJSON 函数读取 json 文件,之前我没有这样做过,我只想让数据显示在我的网页上

这是我的代码

var url = "http://api.openweathermap.org/data/2.5/forecast?lat=35&lon=139&callback=?" ; 
$.getJSON(url, function(res) {
$('#result').html('<p>lon: ' + res.lon + '</p>');

});

这是我的 json 文件

{"coord":
{
"lon":-0.12574,
"lat":51.50853
},
"sys":
{
"country":"GB",
"sunrise":1380780339,
"sunset":1380821577
},
"weather":
[{
"id":521,
"main":"Rain",
"description":"proximity shower rain",
"icon":"09n"
}],
"base":"gdps stations",
"main":
{
"temp":290.43,
"pressure":1008,
"humidity":88,
"temp_min":289.15,
"temp_max":291.48
},
"wind":
{
"speed":3.1,
"deg":140
},
"rain":
{
"1h":1.65
},
"clouds":
{
"all":40
},
"dt":1380823503,
"id":2643743,
"name":"London",
"cod":200
}

最佳答案

那就是city.coord.lon:

var url = "http://api.openweathermap.org/data/2.5/forecast?lat=35&lon=139&callback=?" ; 

$.getJSON(url, function(res) {
$('#result').html('<p>lon: ' + res.city.coord.lon + '</p>');
});

FIDDLE

关于jquery - 使用jquery读取json文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19166674/

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