2016-01-24 86 views
-1

我有例如下面的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

回答

0

您可以使用:

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

1

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

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

這將提取從JSON文本和存儲陣列中陣列。 此外,你可以使用以下來獲得任何價值。

a[0];