我想要显示 json 文件的变量(日期),但它不起作用。我做错了什么?
<script type="text/javascript">
$(document).ready(function()
{
$.getJSON("http://json-head.appspot.com/?url=http://www.trinum.com/ibox/chatel/images/photofull.jpg&callback=?", function(data)
{
$.each(data.headers, function(i,item)
{
if(i < 2)
{
$("body").append("+item.Date+");
}
});
});
});
</script>
最佳答案
实际问题是您的 JSON 响应结构,headers
只是一个对象,并且带有 $.each函数,您正在迭代此对象成员(Content-Length、Via 等...),如果 headers
旨在存储多个对象,您应该使用其上的数组符号“[]
”:
{
"status_code": 200,
"ok": true,
"headers": [{
"Content-Length": "7068",
"Via": "HTTP\/1.1 GWA",
"X-Powered-By": "ASP.NET",
"Accept-Ranges": "bytes",
"X-Google-Cache-Control": "remote-fetch",
"Server": "Microsoft-IIS\/6.0",
"Last-Modified": "Tue, 06 Feb 2007 07:57:38 GMT",
"ETag": "\"8b5f5c78c449c71:2c6a\"",
"Cache-Control": "no-cache",
"Date": "Sun, 19 Jul 2009 05:51:42 GMT",
"Content-Type": "image\/jpeg"
}]
};
这样做,$.each将迭代 headers
数组中定义的整个对象。
数组表示法以[
(左括号)开始,以]
(右括号)结束,值之间用 ,
(逗号)分隔:
有关 JSON 语法和结构检查的更多信息 this site .
关于jquery - 通过jquery显示json变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1147592/