将JSON从[{x:0,y:9},{x:0,y:9} {x:0,y:9} {x:0,y:9}]更改为[[0,9], [0,9] [0,9] [0,9]

时间:2015-07-28 14:05:04

标签: javascript jquery json

我想将我的JSON数据从[{x:0,y:9},{x:0,y:9}{x:0,y:9}{x:0,y:9}]更改为

[[0,9],[0,9][0,9][0,9]]使用javascript或jQuery

2 个答案:

答案 0 :(得分:3)

在javascript中使用 map()



console.log([{
  x: 0,
  y: 9
}, {
  x: 0,
  y: 9
} ,{
  x: 0,
  y: 9
}, {
  x: 0,
  y: 9
}].map(function() {
  return [this.x,this.y];
}));




答案 1 :(得分:0)

首先,这是无效的JSON,有效的JSON将是

[{"x":0,"y":9},{"x":0,"y":9},{"x":0,"y":9},{"x":0,"y":9}]

将JSON字符串更改为数组数组(我假设您不希望它也是JSON - 如果您这样做,只需删除JSON.parse

var result = JSON.parse(oldJSON.replace(/\{\s*("x"|x)\s*:\s*(\d+)\s*,\s*("y"|y)\s*:\s*(\d+)\s*}/gm, '[$1,$2]'));