导入外部"data.json"文件到javascript变量中 [英] Importing external "data.json" file into javascript variable
本文介绍了导入外部"data.json"文件到javascript变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能是重复的,但是我找不到先前回答我的问题.
This may be a duplicate, but I was unable to find a previously asked question that answers mine.
我想像这样将.json文件导入我的javascript:
I am wanting to import a .json file into my javascript like this:
var array = "data.json";
或
var array = $.getJson('data.json');
我知道这两个都是错误的,有人能指出我正确的方向吗?非常欢迎文档.
I know that both of these are wrong, can anyone point me in the right direction? documentation is very welcome.
推荐答案
这是将json文件导入到javascript中的方法.导入json文件后,您可以使用arr
变量来存储json
值.
This is how you import json file to javascript. Once the json file is imported, you can use arr
variable which stores json
value.
var arr = null;
$.ajax({
'async': false,
'global': false,
'url': "/data.json",
'dataType': "json",
'success': function (data) {
arr = data;
}
});
这篇关于导入外部"data.json"文件到javascript变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文