如何在jquery中解析数组中的对象?

时间:2017-01-19 11:33:04

标签: javascript jquery

我从php变量得到json下面的json。现在我如何将这个对象解析成一个数组?

[
    { "fjsd_loc_id": "1", "sum(fjsd_pending_cnt)": "174", "sum(fjsd_running_cnt)": "123", "sum(fjsd_failed_cnt)": "52", "sum(fjsd_success_cnt)": "116" },
    { "fjsd_loc_id": "1", "sum(fjsd_pending_cnt)": "54", "sum(fjsd_running_cnt)": "143", "sum(fjsd_failed_cnt)": "3", "sum(fjsd_success_cnt)": "201" },
    { "fjsd_loc_id": "1", "sum(fjsd_pending_cnt)": "124", "sum(fjsd_running_cnt)": "432", "sum(fjsd_failed_cnt)": "546", "sum(fjsd_success_cnt)": "65" },
    { "fjsd_loc_id": "2", "sum(fjsd_pending_cnt)": "43", "sum(fjsd_running_cnt)": "43", "sum(fjsd_failed_cnt)": "23", "sum(fjsd_success_cnt)": "76" },
    { "fjsd_loc_id": "2", "sum(fjsd_pending_cnt)": "42", "sum(fjsd_running_cnt)": "123", "sum(fjsd_failed_cnt)": "223", "sum(fjsd_success_cnt)": "70" },
    { "fjsd_loc_id": "2", "sum(fjsd_pending_cnt)": "34", "sum(fjsd_running_cnt)": "234", "sum(fjsd_failed_cnt)": "12", "sum(fjsd_success_cnt)": "1" },
    { "fjsd_loc_id": "3", "sum(fjsd_pending_cnt)": "123", "sum(fjsd_running_cnt)": "23", "sum(fjsd_failed_cnt)": "320", "sum(fjsd_success_cnt)": "12" },
    { "fjsd_loc_id": "3", "sum(fjsd_pending_cnt)": "432", "sum(fjsd_running_cnt)": "23", "sum(fjsd_failed_cnt)": "23", "sum(fjsd_success_cnt)": "0" },
    { "fjsd_loc_id": "3", "sum(fjsd_pending_cnt)": "57", "sum(fjsd_running_cnt)": "257", "sum(fjsd_failed_cnt)": "332", "sum(fjsd_success_cnt)": "3" }
]

2 个答案:

答案 0 :(得分:-1)

你需要JSON.parse它(不需要jQuery):

var arr = JSON.parse('[{"fjsd_loc_id":"1","sum(fjsd_pending_cnt)":"174","sum(fjsd_running_cnt)":"123","sum(fjsd_failed_cnt)":"52","sum(fjsd_success_cnt)":"116"},{"fjsd_loc_id":"1","sum(fjsd_pending_cnt)":"54","sum(fjsd_running_cnt)":"143","sum(fjsd_failed_cnt)":"3","sum(fjsd_success_cnt)":"201"},{"fjsd_loc_id":"1","sum(fjsd_pending_cnt)":"124","sum(fjsd_running_cnt)":"432","sum(fjsd_failed_cnt)":"546","sum(fjsd_success_cnt)":"65"},{"fjsd_loc_id":"2","sum(fjsd_pending_cnt)":"43","sum(fjsd_running_cnt)":"43","sum(fjsd_failed_cnt)":"23","sum(fjsd_success_cnt)":"76"},{"fjsd_loc_id":"2","sum(fjsd_pending_cnt)":"42","sum(fjsd_running_cnt)":"123","sum(fjsd_failed_cnt)":"223","sum(fjsd_success_cnt)":"70"},{"fjsd_loc_id":"2","sum(fjsd_pending_cnt)":"34","sum(fjsd_running_cnt)":"234","sum(fjsd_failed_cnt)":"12","sum(fjsd_success_cnt)":"1"},{"fjsd_loc_id":"3","sum(fjsd_pending_cnt)":"123","sum(fjsd_running_cnt)":"23","sum(fjsd_failed_cnt)":"320","sum(fjsd_success_cnt)":"12"},{"fjsd_loc_id":"3","sum(fjsd_pending_cnt)":"432","sum(fjsd_running_cnt)":"23","sum(fjsd_failed_cnt)":"23","sum(fjsd_success_cnt)":"0"},{"fjsd_loc_id":"3","sum(fjsd_pending_cnt)":"57","sum(fjsd_running_cnt)":"257","sum(fjsd_failed_cnt)":"332","sum(fjsd_success_cnt)":"3"}]');

如果你真的想要jQuery:

var $arr = $.parseJSON('[{"fjsd_loc_id":"1","sum(fjsd_pending_cnt)":"174","sum(fjsd_running_cnt)":"123","sum(fjsd_failed_cnt)":"52","sum(fjsd_success_cnt)":"116"},{"fjsd_loc_id":"1","sum(fjsd_pending_cnt)":"54","sum(fjsd_running_cnt)":"143","sum(fjsd_failed_cnt)":"3","sum(fjsd_success_cnt)":"201"},{"fjsd_loc_id":"1","sum(fjsd_pending_cnt)":"124","sum(fjsd_running_cnt)":"432","sum(fjsd_failed_cnt)":"546","sum(fjsd_success_cnt)":"65"},{"fjsd_loc_id":"2","sum(fjsd_pending_cnt)":"43","sum(fjsd_running_cnt)":"43","sum(fjsd_failed_cnt)":"23","sum(fjsd_success_cnt)":"76"},{"fjsd_loc_id":"2","sum(fjsd_pending_cnt)":"42","sum(fjsd_running_cnt)":"123","sum(fjsd_failed_cnt)":"223","sum(fjsd_success_cnt)":"70"},{"fjsd_loc_id":"2","sum(fjsd_pending_cnt)":"34","sum(fjsd_running_cnt)":"234","sum(fjsd_failed_cnt)":"12","sum(fjsd_success_cnt)":"1"},{"fjsd_loc_id":"3","sum(fjsd_pending_cnt)":"123","sum(fjsd_running_cnt)":"23","sum(fjsd_failed_cnt)":"320","sum(fjsd_success_cnt)":"12"},{"fjsd_loc_id":"3","sum(fjsd_pending_cnt)":"432","sum(fjsd_running_cnt)":"23","sum(fjsd_failed_cnt)":"23","sum(fjsd_success_cnt)":"0"},{"fjsd_loc_id":"3","sum(fjsd_pending_cnt)":"57","sum(fjsd_running_cnt)":"257","sum(fjsd_failed_cnt)":"332","sum(fjsd_success_cnt)":"3"}]');

答案 1 :(得分:-1)

假设您需要数组数组而不是对象数组,这取决于映射。一个最简单的例子如下:

var output = input.map(function(currVal) {
  let temp = [];
  temp.push(currVal["fjsd_loc_id"]);
  temp.push(currVal["sum(fjsd_pending_cnt)"]);
  temp.push(currVal["sum(fjsd_running_cnt)"]);
  temp.push(currVal["sum(fjsd_failed_cnt)"]);
  temp.push(currVal["sum(fjsd_success_cnt)"]);
  return temp;
});

然而,这非常容易出错,因为名称中的错误是可能的,并且将在数组中显示未定义,这可能是一个问题。更灵活的解决方案是:

var output2 = input.map(function(currVal) {
  var temp = []; var i = 0;
  var keys = Object.keys(currVal);

  for(i=0; i< keys.length; i++) {
    temp.push(currVal[keys[i]]);
  }

  return temp;
});

然而,这假设您正在使用现代浏览器(IE9 +,FF4 +,Chrome5 +,Opera12 +,Safari5 +)。请参阅How to list the properties of a JavaScript objectObject.keys的更多信息。

var input = [{
  "fjsd_loc_id": "1",
  "sum(fjsd_pending_cnt)": "174",
  "sum(fjsd_running_cnt)": "123",
  "sum(fjsd_failed_cnt)": "52",
  "sum(fjsd_success_cnt)": "116"
}, {
  "fjsd_loc_id": "1",
  "sum(fjsd_pending_cnt)": "54",
  "sum(fjsd_running_cnt)": "143",
  "sum(fjsd_failed_cnt)": "3",
  "sum(fjsd_success_cnt)": "201"
}, {
  "fjsd_loc_id": "1",
  "sum(fjsd_pending_cnt)": "124",
  "sum(fjsd_running_cnt)": "432",
  "sum(fjsd_failed_cnt)": "546",
  "sum(fjsd_success_cnt)": "65"
}, {
  "fjsd_loc_id": "2",
  "sum(fjsd_pending_cnt)": "43",
  "sum(fjsd_running_cnt)": "43",
  "sum(fjsd_failed_cnt)": "23",
  "sum(fjsd_success_cnt)": "76"
}, {
  "fjsd_loc_id": "2",
  "sum(fjsd_pending_cnt)": "42",
  "sum(fjsd_running_cnt)": "123",
  "sum(fjsd_failed_cnt)": "223",
  "sum(fjsd_success_cnt)": "70"
}, {
  "fjsd_loc_id": "2",
  "sum(fjsd_pending_cnt)": "34",
  "sum(fjsd_running_cnt)": "234",
  "sum(fjsd_failed_cnt)": "12",
  "sum(fjsd_success_cnt)": "1"
}, {
  "fjsd_loc_id": "3",
  "sum(fjsd_pending_cnt)": "123",
  "sum(fjsd_running_cnt)": "23",
  "sum(fjsd_failed_cnt)": "320",
  "sum(fjsd_success_cnt)": "12"
}, {
  "fjsd_loc_id": "3",
  "sum(fjsd_pending_cnt)": "432",
  "sum(fjsd_running_cnt)": "23",
  "sum(fjsd_failed_cnt)": "23",
  "sum(fjsd_success_cnt)": "0"
}, {
  "fjsd_loc_id": "3",
  "sum(fjsd_pending_cnt)": "57",
  "sum(fjsd_running_cnt)": "257",
  "sum(fjsd_failed_cnt)": "332",
  "sum(fjsd_success_cnt)": "3"
}];

var output = input.map(function(currVal) {
  var temp = [];
  temp.push(currVal["fjsd_loc_id"]);
  temp.push(currVal["sum(fjsd_pending_cnt)"]);
  temp.push(currVal["sum(fjsd_running_cnt)"]);
  temp.push(currVal["sum(fjsd_failed_cnt)"]);
  temp.push(currVal["sum(fjsd_success_cnt)"]);
  return temp;
});

var output2 = input.map(function(currVal) {
  var temp = []; var i = 0;
  var keys = Object.keys(currVal);
  
  for(i=0; i< keys.length; i++) {
    temp.push(currVal[keys[i]]);
  }
  
  return temp;
});


console.log(output2);