$.getJSON('dat.js', function(i,data) {
$( "#lessons" ).tmpl( data[1].title ).appendTo( "#result" );
});
我的json文件
{
"posts": [
{
"title": "ajax | Programming ",
"url": "hello"
},
{
"title": "jQuery and Ajax Demos Pard - 3",
"url": "how are you"
},
]
}
我想访问名称字段,但数据[0]显示错误为未定义,
答案 0 :(得分:1)
您的JSON似乎无效。键必须用引号括起来,因此每个对象看起来都是
{"Name" : "Foo", "ReleaseYear" : "Bar"}
使用JSONLint.com验证JSON字符串。
<强> Live Demo 强>
$(document).ready(function(){
var test = '[{ "Name": "The Red Violin", "ReleaseYear": "1998" },{ "Name": "Eyes Wide Shut", "ReleaseYear": "1999" },{ "Name": "The Inheritance", "ReleaseYear": "1976" }]';
var obj = JSON.parse(test);
console.log(obj[0].Name);
});