Loading local JSON file

前端 未结 23 2019
悲哀的现实
悲哀的现实 2020-11-22 01:28

I\'m trying to load a local JSON file but it won\'t work. Here is my JavaScript code (using jQuery):

var json = $.getJSON("test.json");
var data = e         


        
23条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 01:51

    If you are using a local array for JSON - as you showed in your example in the question (test.json) then you can is the parseJSON() method of JQuery ->

    var obj = jQuery.parseJSON('{"name":"John"}');
    alert( obj.name === "John" );
    

    getJSON() is used for getting JSON from a remote site - it will not work locally (unless you are using a local HTTP Server)

提交回复
热议问题