【问题标题】:using jquery to read json file使用jquery读取json文件
【发布时间】:2013-10-03 18:37:33
【问题描述】:

我正在尝试使用 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
     }

【问题讨论】:

  • 您在尝试加载时遇到 CORS 错误吗?

标签: jquery json


【解决方案1】:

那就是 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

【讨论】:

  • 有效,但城市拍拍意味着我在 json 文件中看不到它是什么意思?
  • 那你看的不够仔细,因为它就在那里。
  • 得到它的例子与 URI 不同
【解决方案2】:

以下网址:

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

在 JSON 输出前添加 ?

如果您从 URL 字符串中删除 callback 参数。您应该能够阅读 JSON 对象。

【讨论】:

  • 如果是跨域请求且未使用 CORS,则不会
猜你喜欢
  • 1970-01-01
  • 2020-02-29
  • 1970-01-01
  • 2011-07-02
  • 2015-01-19
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 2019-07-04
相关资源
最近更新 更多