JSON from .NEt WCF to JavaScript array of arrays

我的未来我决定 提交于 2019-12-13 03:34:46

问题


I'm using jqPlot and I need to turn this JSON which I receive from a WCF service:

[{ "x": 2, "y": 3 }, { "x": 25, "y": 34 }]

into this array or arrays:

[[2,3],[25,34]]

I've tried JSON.parse & eval but to no avail.

thanks


回答1:


You can use $.map() to do that:

var data = [{ "x": 2, "y": 3 }, { "x": 25, "y": 34 }]

var flattenedResult = $.map(data, function(point) {
  return [[ point.x, point.y ]];
});



回答2:


Parse the string into an array of objects:

var json = '[{ "x": 2, "y": 3 }, { "x": 25, "y": 34 }]';
var o = $.parseJSON(json);

Then replace each object in the array with an array:

for (var i=0; i<o.length; i++) o[i] = [o[i].x, o[i].y];  


来源:https://stackoverflow.com/questions/5238677/json-from-net-wcf-to-javascript-array-of-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!