如何解析JSON对象数组并从中创建数组

时间:2015-11-05 14:26:28

标签: jquery

我正在研究Google图表API和Google Visualization烛台图表,期望数据是一个数组数组,以便它可以工作。

从后端我以这种格式回收数据

[  
   {  
      "current_day":"2011-08-01",
      "open_val":136.65,
      "high_val":136.96,
      "low_val":134.15,
      "close_val":134.15
   },
   {  
      "current_day":"2011-08-02",
      "open_val":135.26,
      "high_val":135.95,
      "low_val":131.50,
      "close_val":131.85
   }
]

您能否告诉我如何将此数据构建为以下格式

[
[ "2011-08-01", 136.65, 136.96, 134.15, 136.49 ],
[ "2011-08-02", 135.26, 135.95, 131.50, 131.85 ]
]

将其提供给谷歌图表中的烛台输入格式数据。

我的小提琴

http://jsfiddle.net/Lm5mLg1d/

3 个答案:

答案 0 :(得分:1)

根据定义,您需要按照JavaScript object properties are unordered的顺序列出属性列表。

有了这个,它很简单:

var props = [
  "current_day", "open_val", "high_val", "low_val", "close_val"
];

var data = [  
   {  
      "current_day":"2011-08-01",
      "open_val":136.65,
      "high_val":136.96,
      "low_val":134.15,
      "close_val":134.15
   },
   {  
      "current_day":"2011-08-02",
      "open_val":135.26,
      "high_val":135.95,
      "low_val":131.50,
      "close_val":131.85
   }
]

var arrays = data.map(
  function (o) {
    var array = [];
    
    props.forEach(
      function (prop)
      {
        array.push( o[prop] );
      }
    );
    
    return array;
  }
);

console.log(arrays);

或者,对于缺少map()forEach()的旧浏览器:

var props = [
  "current_day", "open_val", "high_val", "low_val", "close_val"
];

var data = [  
   {  
      "current_day":"2011-08-01",
      "open_val":136.65,
      "high_val":136.96,
      "low_val":134.15,
      "close_val":134.15
   },
   {  
      "current_day":"2011-08-02",
      "open_val":135.26,
      "high_val":135.95,
      "low_val":131.50,
      "close_val":131.85
   }
]

var arrays = [];

for ( var i = 0; i < data.length; ++i )
  {
    var array = [];
    
    for ( var j = 0; j < props.length; ++j )
      {
        array.push( data[i][props[j]] );
      }
    
    arrays.push(array);
  }

console.log(arrays);

答案 1 :(得分:0)

你可以试试这个

您需要映射数组,因此每个新术语都是包含对象的每个键的值,因此:

var stuff = [
   {  
      "current_day":"2011-08-01",
      "open_val":136.65,
      "high_val":136.96,
      "low_val":134.15,
      "close_val":134.15
   },
   {  
      "current_day":"2011-08-02",
      "open_val":135.26,
      "high_val":135.95,
      "low_val":131.50,
      "close_val":131.85
  }
];

var result = stuff.map(function (object) { 
    var arr = [];
  for(var prop in object) {
    arr.push(object[prop]);
  }
  return arr;
});

alert(result);

答案 2 :(得分:0)

您可以简单地编写一个函数来实现此目的。像下面这样的东西应该有效:

var myJson = [{
    "current_day": "2011-08-01",
        "open_val": 136.65,
        "high_val": 136.96,
        "low_val": 134.15,
        "close_val": 134.15
}, {
    "current_day": "2011-08-02",
        "open_val": 135.26,
        "high_val": 135.95,
        "low_val": 131.50,
        "close_val": 131.85
}];
var myArr = [];
var i = 0;
for (i = 0; i < myJson.length; i++) {
    myArr.push({
        myJson.current_day, myJson.open_val, myJson.high_val, myJson.low_val, myJson.close_val
    });
}