使用jQuery读取JSON文件
我的JSON文件如下所示:使用jQuery读取JSON文件,jquery,json,Jquery,Json,我的JSON文件如下所示: [ { "course": "CMPT 102 D1", "instructor": "hamarneh", "students": ["axc5", "yqt6"], "title": "Scientific Cmpt.Prgm" }, { "course": "CMPT 120 D1", "instructor": "diana", "students": ["axc5", "ji12
[
{
"course": "CMPT 102 D1",
"instructor": "hamarneh",
"students": ["axc5", "yqt6"],
"title": "Scientific Cmpt.Prgm"
},
{
"course": "CMPT 120 D1",
"instructor": "diana",
"students": ["axc5", "ji12"],
"title": "Intro.Cmpt.Sci/Programming I"
}]
您好,您可以使用以下功能读取文件
function readFile(fileLocation,callback){
$.getJSON(fileLocation)
.done(function( json ) {
console.log( "JSON Data: "+JSON.stringify(json));
callback(json);
})
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
callback(null);
});
}
这是通话部分
第一个参数是文件位置,无论是本地还是URL
第二个参数是回调函数
readFile('demo.json',function(result){
console.log("result "+result);
});
解析完成后,您将收到回调
有关$.getJSON的更多详细信息,请访问jQuery官方网站。
数组[0]。学生[0]
作为对象的项目有什么问题?感谢您的回复,它很有效。感谢您的重播。