javascript 如何使用 getJSON 从文件中获取 JSON 数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14916940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get JSON array from file with getJSON?
提问by adeneo
How could I get an array json of a json file with javascript and jquery
如何使用 javascript 和 jquery 获取 json 文件的数组 json
I was triyng with the next code, with it doesnt work:
我正在尝试使用下一个代码,但它不起作用:
var questions = [];
function getArray(){
$.getJSON('questions.json', function (json) {
for (var key in json) {
if (json.hasOwnProperty(key)) {
var item = json[key];
questions.push({
Category: item.Category
});
}
}
return questions;
})
}
this is the json file called: questions.json
这是名为的 json 文件:questions.json
{
"Biology":{
"Category":{
"cell":{
"question1":{
"que1":"What is the cell?"
},
"option1":{
"op1":"The cell is the basic structural and functional unit",
"op2":"is a fictional supervillain in Dragon Ball"
},
"answer1":"opt1"
}
}
},
"Astronomy":{
"Category":{
"Mars":{
"question1":{
"que1":"How many moons does Mars?"
},
"option1":{
"op1":"5",
"op2":"2"
},
"answer1":"opt2"
}
}
}
}
I want to get an array with this format {Biology:{Category:{cell:{question1....}}}}
我想获得这种格式的数组 {Biology:{Category:{cell:{question1....}}}}
回答by adeneo
$.getJSON
is an asynchronous function, so returning something inside that function does nothing, as it's not in scope, or received yet. You should probably do something like:
$.getJSON
是一个异步函数,因此返回该函数内部的某些内容不会执行任何操作,因为它不在范围内或尚未收到。您可能应该执行以下操作:
function getArray(){
return $.getJSON('questions.json');
}
getArray().done(function(json) {
// now you can use json
var questions = [];
$.each(json, function(key, val) {
questions[key] = { Category: val.Category };
});
});
回答by nbrooks
Your conditional within the for
loop prevents anything from being added to your array. Instead, check if your json object has the property, then get the value and add it to your array. In other words:
您在for
循环中的条件会阻止任何内容添加到您的数组中。相反,检查您的 json 对象是否具有该属性,然后获取该值并将其添加到您的数组中。换句话说:
if (questions.hasOwnProperty(key))
should be if (json.hasOwnProperty(key))
if (questions.hasOwnProperty(key))
应该 if (json.hasOwnProperty(key))
Also, you can't simply return
the result of an AJAX call like that, because the method runs asynchronously. That return
is actually applied to the inner success
function callback, not getArray
. You have to use a callback pattern in order to pass the data only once it has been received, and operate on it accordingly.
此外,您不能像那样简单地return
获得 AJAX 调用的结果,因为该方法是异步运行的。这return
实际上应用于内部success
函数回调,而不是getArray
. 您必须使用回调模式,以便仅在收到数据后传递数据,并相应地对其进行操作。
(Of course since the array is defined in the outer scope you wouldn't have to return it anyway, but if you attempted to use it before the AJAX method ended it would be empty.)
(当然,由于数组是在外部作用域中定义的,因此无论如何您都不必返回它,但是如果您尝试在 AJAX 方法结束之前使用它,它将为空。)
Assuming you are going to render it to the DOM using a method called renderJSON
:
假设您将使用名为 的方法将其渲染到 DOM renderJSON
:
var questions = [];
function getArray(){
$.getJSON('questions.json', function (json) {
for (var key in json) {
if (json.hasOwnProperty(key)) {
var item = json[key];
questions.push({
Category: item.Category
});
}
}
renderJSON(questions);
});
}