So what would be the best practice to parse json output in a situation like this one. Do I really need to go through 2+ (depending on the depth of the data) $.each loops? I’m a bit new to javascript.
$.getJSON(url, null, function(data) {
$.each(data, function(i,item){
$.each(item,function(a,b) {
if (a == 'poll_date') {
dates.push(b);
} else if (a == 'prix') {
prices.push(parseFloat(b));
}
$("#testout").append("A:" + a + " B:" + b + "<br>");
});
});
});
JSON Sample:
[
{"poll_date":"1990-01-01","prix":"54.60"},
{"poll_date":"1990-02-01","prix":"55.40"},
{"poll_date":"1990-03-01","prix":"58.90"},
{"poll_date":"1990-04-01","prix":"58.90"},
{"poll_date":"1990-05-01","prix":"59.30"}
]
How about:
jsFiddle example. Plus it uses plain JavaScript.