转换JSON字符串数组

问题描述:

我有例如下面的JSON字符串:转换JSON字符串数组

'{"objects":[{"type":"rect","originX":"left","originY":"top","left":225,"top":155,"width":100,"height":50,"fill":"#000","stroke":"blue","strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","selectable":true,"id":1,"rx":0,"ry":0},{"type":"rect","originX":"left","originY":"top","left":342,"top":81,"width":100,"height":50,"fill":"#000","stroke":"blue","strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","selectable":true,"id":2,"rx":0,"ry":0},{"type":"rect","originX":"left","originY":"top","left":90,"top":138,"width":100,"height":50,"fill":"#000","stroke":"blue","strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","selectable":"true","id":1,"rx":0,"ry":0},{"type":"rect","originX":"left","originY":"top","left":401,"top":271,"width":100,"height":50,"fill":"#000","stroke":"blue","strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","selectable":true,"id":2,"rx":0,"ry":0}],"background":""}' 

我需要将其存储在一个JavaScript数组的格式如下:

[ { type: 'rect', 
originX: 'left', 
originY: 'top', 
left: 90, 
top: 138, 
width: 100, 
height: 50, 
fill: '#000', 
stroke: 'blue', 
strokeWidth: 1, 
strokeDashArray: null, 
strokeLineCap: 'butt', 
strokeLineJoin: 'miter', 
strokeMiterLimit: 10, 
scaleX: 1, 
scaleY: 1, 
angle: 0, 
flipX: false, 
flipY: false, 
opacity: 1, 
shadow: null, 
visible: true, 
clipTo: null, 
backgroundColor: '', 
fillRule: 'nonzero', 
globalCompositeOperation: 'source-over', 
rx: 0, 
ry: 0 }, 
{ type: 'rect', 
originX: 'left', 
originY: 'top', 
left: 401, 
top: 271, 
width: 100, 
height: 50, 
fill: '#000', 
stroke: 'blue', 
strokeWidth: 1, 
strokeDashArray: null, 
strokeLineCap: 'butt', 
strokeLineJoin: 'miter', 
strokeMiterLimit: 10, 
scaleX: 1, 
scaleY: 1, 
angle: 0, 
flipX: false, 
flipY: false, 
opacity: 1, 
shadow: null, 
visible: true, 
clipTo: null, 
backgroundColor: '', 
fillRule: 'nonzero', 
globalCompositeOperation: 'source-over', 
rx: 0, 
ry: 0 } ] 

有什么事情可能吗?它基本上是我的应用程序需要的一组对象。如果你想知道,json字符串包含画布上的对象。

+4

所以,'VAR resultArray = JSON.parse(yourJSON).objects;'? – nnnnnn

+0

我得到[object Object],[object Object],[object Object],[object Object],[object Object] –

+0

如果您调用console.log(resultArray.toString()), 。使用resultArray [0] .type访问数组中第一个对象的属性'type'。 – grabantot

您可以使用:

JSON.parse

Some documentation

,比提取objects值:

JSON.parse(yourJSONstring).objects

要访问单objec内部objects阵列可以吨:

var objectsArray = JSON.parse(yourJSONstring).objects; 
var singleArray = objectsArray[0]; //for first object in array; 

WORKING EXAMPLE

可以使用JSON.parse()来方法如下:

var a = JSON.parse("json text OR json variable")['objects']; 

这将提取从JSON文本和存储阵列中阵列。 此外,你可以使用以下来获得任何价值。

a[0];