Colon Posted December 14, 2018 Share Posted December 14, 2018 I am trying to read a json file in my js with ajax. And I am not sure about that json file, what is an object, what is an array. .json file "{ "people": [ { "answers": [ { "subject": "home", "suggest": 2 }, { "subject": "livelihood", "suggest": 2 }, { "subject": "childhood", "suggest": 1 } ], "class": { "detail": "commoner", "exclusions": [ { "category": "childEvents", "detail": "apprentice" } ], "selectWeight": 0.889 }, "correlations": [], "default-ask": [ { "text": "a text", "topics": [] } ], ..... } ] } people is an array, in people we have one object which contains all the following stuff (answers, correlations, default-ask,...)? answers is an array in people in answers are 3 objects. the next is class is that an object which contains an id detail, a value selectWeight and an array exclusions? then an other array correlations and an array default-ask with an object with the id text and an array topics I know how to read out answers but not how to read out the rest. can you help me with that please. .js file $.ajax({ url: 'assets/articles.json', dataType: 'json', type: 'get', cache: false, success: function(data){ $(data.people[0].answers).each(function(index, value){ console.log(value.subject + ' ' + value.suggest) }) } }); Link to comment Share on other sites More sharing options...
Colon Posted December 14, 2018 Author Share Posted December 14, 2018 try this $.ajax({ url: 'assets/articles.json', dataType: 'json', type: 'get', cache: false, success: function(data){ console.log(data.people[0].class.detail); console.log(data.people[0].class.selectWeight); $(data.people[0].class.exclusions).each(function(index, value){ console.log(value.category + ' ' + value.detail) }) } }); oh cool, thanks ? Link to comment Share on other sites More sharing options...
hektor74 Posted December 15, 2018 Share Posted December 15, 2018 If you don't use jquery you may try this: function eAjaxRequest(url, completeFunction, params) { var request = this; this.requestUrl = url; this.callback = completeFunction || function () { }; this.postParams = params; this.onprogress = function() { }; this.post = function(params){ request.http = null; var activexmodes = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]; if (window.XMLHttpRequest){ // if Mozilla, Safari etc request.http = new XMLHttpRequest(); }else if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken) for (var i=0; i < activexmodes.length; i++){ try{ request.http = new ActiveXObject(activexmodes[i]); } catch(e){ console.log(e); //suppress error } } } if (request.http == null) { return false; }else{ request.http.onprogress = function(evt){ request.onprogress(String(evt.loaded / evt.total)); } request.http.onreadystatechange = function(){ if(request.http.readyState == 4){ if(this.status == 200 || window.location.href.indexOf("http") == -1){ request.callback(request.http.responseText, request.http.status, request.http.responseXML); request.http = null; }else{ console.log("Error loading Ajax data"); } } } request.http.open("POST", this.requestUrl, true); request.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); if(params){ request.http.setRequestHeader("Content-Length", params.length); request.http.send(params); }else{ request.http.send(); } return true; } } this.post(params); } Link to comment Share on other sites More sharing options...
Recommended Posts