i have a little problem, i am a beginner in programming. I want to work with the Erast API, a Formula 1 API. I want to get all the drivers who won single races, the request is the following: http://ergast.com/api/f1/current/results/1.json
Structure of the return values:
Problem is, that i dont know how to parse the JSON into a JS Array, I thought something like that:
var names = [];
var index = 0;
$.getJSON("http://ergast.com/api/f1/current/results/1.json", function(data, status) {
$.each(data.MRData.RaceTable.Races, function(name, value) {
//names.push(value.Results[0].Driver.givenName + " " + value.Results[0].Driver.familyName));
obj = $.parseJSON(value);
names.push(obj.Results[0].Driver.givenName);
//console.log(value.Results[0].Driver.givenName+ " " + value.Results[0].Driver.familyName);
});
});
If anyone knows the answer, would appreciate hearing from you
$.parseJSONon the whole JSON string and then iterate through the results. You're trying to accessdata.MRDatabefore you've parsed the string.parseJSON()call in the loop is necessary.$.getJSONinstead of$.get(). Thanks @trincot. In that case you may not need to use parseJSON inside the loop at all.