Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jQuery读取JSON文件_Jquery_Json - Fatal编程技术网

使用jQuery读取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

我的JSON文件如下所示:

[
  {
    "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"
  }]
  • 我想把这个文件转换成一个变量,这样我就可以操纵它了。 例如,var array=JSON文件
  • 转换为数组后,如何访问学生姓名?它是数组[0][2][0]=“axc5”吗
  • 我尝试了getJSON,但失败了,它将项目作为对象而不是字符串输出

  • 您好,您可以使用以下功能读取文件

     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]
    作为对象的项目有什么问题?感谢您的回复,它很有效。感谢您的重播。