问题
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