使用Jquery读出JSON值

时间:2012-11-24 17:40:24

标签: javascript jquery json

我正在尝试从JSON提要创建2个数组,然后计算两者的平均值。首先,我对数组的方式是这样的:

$.getJSON('jasonfile.json', function(rawdata) {
  var array_one = [];
  var array_two = [];

  $.each(rawdata, function(i, entry) {
    if(entry["geslacht"] == "Man") {
      array_one.push(entry["key"]);
    else {
      array_two.push(entry["key"]);
    }
  )};
)};

这很好用,除非我查看我的控制台array_one似乎是某些对象数组,而`array_two似乎是一个普通的数组。因此当我尝试计算两者的平均值时:

var array_one_total = 0;
$.each(array_one, function() {
  array_one_total += this;
});

array_one_average = array_one_total/array_one.length;

我得到了正确和错误的价值。 array_one在我的控制台中记录“NaN”,而array_two显示正确的平均值:

Javascript weird result in console

这似乎是数组格式的问题,但我无法理解为什么返回第二个值是正确的,因为我对它们使用相同的方法。

如果我将两个数组直接记录到控制台,您会看到它们显示的不同方式,这就是图像:

console log

我收到很多回复询问我的整个代码,数组的数量让它更难阅读,但现在是:

$.getJSON('propedeuse201112.json', function(rawdata) {
  var ucd_male = [];
  var ucd_female = [];
  var internetstandaarden_male = [];
  var internetstandaarden_female = [];
  var understanding_design_male = [];
  var understanding_design_female = [];
  var taal_in_context_male = [];
  var taal_in_context_female = [];
  var vormgeving_male = [];
  var vormgeving_female = [];
  var marketing_male = [];
  var marketing_female = [];
  var h_en_c_male = [];
  var h_en_c_female = [];
  var programmeren_male = [];
  var programmeren_female = [];
  var m_en_i_male = [];
  var m_en_i_female = [];
  var mediageschiedenis_male = [];
  var mediageschiedenis_female = [];
  var business_mapping_male = [];
  var business_mapping_female = [];
  var plug_and_play_male = [];
  var plug_and_play_female = [];
  var slc_male = [];
  var slc_female = [];

  $.each(rawdata, function(i, entry) {
    if(entry["geslacht"] == "Man") {
      ucd_male.push(entry["ucd"]);
      internetstandaarden_male.push(entry["internetstandaarden"]);
      understanding_design_male.push(entry["understanding design"]);
      taal_in_context_male.push(entry["Taal in context"]);
      vormgeving_male.push(entry["Vormgeving"]);
      marketing_male.push(entry["Marketing"]);
      h_en_c_male.push(entry["H&C"]);
      programmeren_male.push(entry["Programmeren"]);
      m_en_i_male.push(entry["M&I"]);
      mediageschiedenis_male.push(entry["Mediageschiedenis"]);
      business_mapping_male.push(entry["Business Mapping"]);
      plug_and_play_male.push(entry["Plug & Play"]);
      slc_male.push(entry["SLC"]);
    } else {
      ucd_female.push(entry["ucd"]);
      internetstandaarden_female.push(entry["internetstandaarden"]);
      understanding_design_female.push(entry["understanding design"]);
      taal_in_context_female.push(entry["Taal in context"]);
      vormgeving_female.push(entry["Vormgeving"]);
      marketing_female.push(entry["Marketing"]);
      h_en_c_female.push(entry["H&C"]);
      programmeren_female.push(entry["Programmeren"]);
      m_en_i_female.push(entry["M&I"]);
      mediageschiedenis_female.push(entry["Mediageschiedenis"]);
      business_mapping_female.push(entry["Business Mapping"]);
      plug_and_play_female.push(entry["Plug & Play"]);
      slc_female.push(entry["SLC"]);
    }

  });

  var ucd_male_total = 0;
  $.each(ucd_male, function(index,item) {
    ucd_male_total += parseInt(item);
  });
  ucd_male_average = ucd_male_total/ucd_male.length;

  var ucd_female_total = 0;
  $.each(ucd_female, function(index,item) {
    ucd_female_total += parseInt(item);
  });
  ucd_female_average = ucd_female_total/ucd_female.length;

  console.log(ucd_male);
  console.log(ucd_female);  

});

rawdata的结构下面(这是一个条目):

[{"id":1,"geslacht":"Vrouw","totaal punten":60,"project      1":9,"ucd":6,"internetstandaarden":7,"understanding design":7,"Teamproject deel 1":6,"Taal in context":6,"Vormgeving":7,"Marketing":6,"Students in Motion":7,"Teamproject deel 2":7,"H&C":6,"Programmeren":7,"M&I":7,"Project 3":6,"Mediageschiedenis":6,"Business Mapping":7,"Plug & Play":7,"SLC":7,"Studieregie":8,"plaats":"JOPPE","geboortejaar":1990,"vooropleiding":"HAVO","vooropleiding afgerond in":2009},

3 个答案:

答案 0 :(得分:2)

看起来您的代码包含一些拼写错误。

var array_one_total = 0;
$.each(array_one, function() {
  array_one += this;
});

它无法运作。我想你想这样做:

var array_one_total = 0;
$.each(array_one, function(index, value) {
  array_one_total += value;
});

您谈到的是array_two,但我只看到一个关于此array_two的代码行。所以我不知道array_two可以包含任何内容。

答案 1 :(得分:1)

您的代码中有几个关键的拼写错误

 $.each(rawdata, function(i, entry) {
    if(entry["geslacht"] == "Man") {
      array_one.push(entry["key"]);
    else {
      array_one.push(entry["key"]); /* <===== should be array_two*/
    }
  )};

var array_one_total = 0;
$.each(array_one, function() {
  array_one += this;/* <===== should be array_one_total*/
});

答案 2 :(得分:0)

似乎问题在于this。请尝试关注。

$.each(array_one, function(index,item) {
     array_one += parseInt(item);
});

还要检查在分割之前array_one是否为NAN。