我有例如下面的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字符串包含畫布上的對象。
所以,'VAR resultArray = JSON.parse(yourJSON).objects;'? – nnnnnn
我得到[object Object],[object Object],[object Object],[object Object],[object Object] –
如果您調用console.log(resultArray.toString()), 。使用resultArray [0] .type訪問數組中第一個對象的屬性'type'。 – grabantot