javascript - How to get JSON array from file with getJSON? -
javascript - How to get JSON array from file with getJSON? -
how array json of json file javascript , jquery
i triyng next code, doesnt work:
var questions = []; function getarray(){ $.getjson('questions.json', function (json) { (var key in json) { if (json.hasownproperty(key)) { var item = json[key]; questions.push({ category: item.category }); } } homecoming questions; }) }
this json file called: questions.json
{ "biology":{ "category":{ "cell":{ "question1":{ "que1":"what cell?" }, "option1":{ "op1":"the cell basic structural , functional unit", "op2":"is fictional supervillain in dragon ball" }, "answer1":"opt1" } } }, "astronomy":{ "category":{ "mars":{ "question1":{ "que1":"how many moons mars?" }, "option1":{ "op1":"5", "op2":"2" }, "answer1":"opt2" } } } }
i want array format {biology:{category:{cell:{question1....}}}}
$.getjson
asynchronous function, returning within function nothing, it's not in scope, or received yet. should like:
function getarray(){ homecoming $.getjson('questions.json'); } getarray().done(function(json) { // can utilize json var questions = []; $.each(json, function(key, val) { questions[key] = { category: val.category }; }); });
javascript jquery arrays json
Comments
Post a Comment