如何创建从外部文件JSON JavaScript数组 [英] How to create JAVASCRIPT ARRAY from external file JSON
问题描述
什么是创建JSON文件JavaScript数组的最佳方式?
我有四个空的JavaScript数组,我想从一个JSON文件导入的数据来填充。
VAR姓=新的Array();
VAR地址=新的Array();
VAR城市=新的Array();
文件JSON:file.json
[
{名:约翰,地址:350第五大道,城市:纽约},
{名:马克,地址:1101拱圣,城市:费城},
{名:杰克,地址:60街,城市:芝加哥}
]
我尝试:
$。的getJSON('file.json'功能(数据){ 对于(VAR I = 0; I< data.length;我++){
firstname.push.apply(名字,数据[I] .firstname);
address.push.apply(地址,数据[I]。地址);
city.push.apply(市,数据[I]。城市);
}});
但是数组仍然是空的。
在此先感谢
================================解决============== =================
$。阿贾克斯({
异步:假的,
网址:'file.json',
数据:,
接受:应用/ JSON,
数据类型:JSON,
成功:功能(数据){
对于(VAR I = 0; I< data.length;我++){
firstname.push(数据[I] .firstname);
address.push(数据[I]。地址);
city.push(数据[I]。城市);
}
}
})
//获取回调函数外阵列
的console.log('结果:+ firstname.length); //结果:3
您有两个或三个问题。
您的JSON是无效的。
这是围绕它的外面取下报价。
认真考虑给予一个以.json
文件扩展名,那么你就更有可能得到正确的MIME类型JSON。 (JSON数据文件不是JavaScript程序)。
文件JSON:data.json
[
{名:约翰,地址:350第五大道,城市:纽约},
{名:马克,地址:1101拱圣,城市:费城},
{名:杰克,地址:60街,城市:芝加哥}
]
您正在访问错误的顺序数据
这是过于复杂,在错误的顺序访问属性。
name.push.apply(姓名,data.name [I]);
忘记使用适用
。你不需要它。
然后注意,您的JSON数组包含对象,而不是周围的其他方法。您需要在属性名前以访问数组索引:
name.push(数据[I]。名称);
记住Ajax中的A
最后 - 您的code不显示你如何测试的结果。请记住,阿贾克斯是异步的,因此,如果您检查回调函数以外的阵列的价值观,他们可能没有被填充呢。
What is the best way to create javascript array from json file? I have four empty JavaScript array that I would like to populate with data imported from a json file.
var firstname = new Array();
var address= new Array();
var city = new Array();
File Json : "file.json"
[
{"name ": "John", "address": "350 Fifth Avenue", "city ": "New York"},
{"name ": "Mark", "address": "1101 Arch St", "city ": "Philadelphia"},
{"name ": "Jack", "address": "60th Street", "city ": "Chicago"}
]
I try:
$.getJSON('file.json',function (data) {
for (var i = 0; i < data.length; i++) {
firstname.push.apply( firstname, data[i].firstname );
address.push.apply( address, data[i].address );
city.push.apply( city, data[i].city );
}
});
but arrays are still empty. Thanks in advance
================================ SOLVED ===============================
$.ajax({
async: false,
url: 'file.json',
data: "",
accepts:'application/json',
dataType: 'json',
success: function (data) {
for (var i = 0; i < data.length; i++) {
firstname.push( data[i].firstname );
address.push( data[i].address );
city.push( data[i].city );
}
}
})
// Get arrays outside callback function
console.log('result: '+firstname.length); // result: 3
You have two or three problems.
Your JSON is invalid.
Remove the quotes from around the outside of it.
Seriously consider giving is a .json
file extension, then you are more likely to get the right MIME type for JSON. (JSON data files are not JavaScript programs).
File Json : "data.json"
[
{"name ": "John", "address": "350 Fifth Avenue", "city ": "New York"},
{"name ": "Mark", "address": "1101 Arch St", "city ": "Philadelphia"},
{"name ": "Jack", "address": "60th Street", "city ": "Chicago"}
]
You are accessing your data in the wrong order
This is overly complicated and is accessing properties in the wrong order.
name.push.apply(name, data.name[i]);
Forget about using apply
. You don't need it.
Then note that your JSON array contains the objects, not the other way around. You need to access the array index before the property name:
name.push(data[i].name);
Remember the A in Ajax
Finally - your code doesn't show how you are testing the result. Remember that Ajax is asynchronous, so that if you examine the values of your arrays outside the callback function, they might not have been populated yet.
这篇关于如何创建从外部文件JSON JavaScript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!