因此,在这种情况下解析json输出的最佳实践是什么。我真的需要遍历2+(取决于数据的深度)$。each循环吗?我对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示例:

[
 {"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"}
]

最佳答案

怎么样:

var dates = new Array();
var prices = new Array();
for (foo in data) {
    dates.push(data[foo].poll_date);
    prices.push(data[foo].prix);
}


jsFiddle example。另外,它使用普通的JavaScript。

10-06 14:08