将JSON字符串转换为数组

时间:2016-01-24 13:07:38

标签: javascript arrays 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字符串包含画布上的对象。

2 个答案:

答案 0 :(得分:1)

您可以按如下方式使用JSON.parse()方法:

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

这将从Json文本中提取数组并存储在数组a中。 此外,你可以使用以下来获取任何价值。

a[0];

答案 1 :(得分:0)

您可以使用:

JSON.parse

Some documentation

然后提取objects

的值

JSON.parse(yourJSONstring).objects

要访问objects数组中的单个对象,您可以:

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

WORKING EXAMPLE