如何加载本地JSON文件?
我试图通过jquery加载本地JSON文件。 代码工作正常但数据在数组中不可用。
$.getJSON("/ajax/data/myjasonfile.json", function(json) { console.log(json); });
我的控制台只显示
Object {data: Array[1]}
我的样本json
{ "data": [ { "number": "1234", "nameOne": "Laten Thamn", "type": "Fishing Vessels", "flagName": "5467", "buildDate":"12/08/2016" } ] }
试试这个来理解你的反应结构。
$.getJSON("myjasonfile.json", function(json) { console.log(json); // access the response object console.log(json.data); // access the array console.log(json.data[0]); // access the first object of the array console.log(json.data[0].number); // access the first object proprty of the array });
jQuery.getJSON()使用GET HTTP请求从服务器加载JSON编码的数据。
当您在JSON结构中发布时,可以通过调用json.data
来访问该数组:
$.getJSON("/ajax/data/myjasonfile.json", function(json) { console.log(json.data); // Logs your array });
将json数据存储在数组中:
$.getJSON("/ajax/data/myjasonfile.json", function(Data) { var array=[]; for(var i=1;i
你可以试试这个:
$.getJSON("test.json", function(json) { console.log(json); // this will show the info it in firebug console });
我想,有些问题在你的路径中调用json文件。 你需要再次检查路径。
您可以查看以下链接: 加载本地JSON文件