Javascript 将JSON字符串转换为数组

Javascript 将JSON字符串转换为数组,javascript,arrays,json,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","strokeMite

例如,我有以下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字符串包含画布上的对象。

您可以使用json.parse()方法,如下所示:

var a = JSON.parse("json text OR json variable")['objects'];
这将从Json文本中提取数组并存储在数组a中。 此外,您可以使用以下命令从中获取任何值

a[0];
您可以使用:

JSON.parse

然后提取
对象的值

JSON.parse(yourJSONstring.objects

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

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

So,
var resultArray=JSON.parse(yourJSON.objects)?我得到的是[object object]、[object object object]、[object object]、[object object object]、[object object object],这正是调用
console.log(resultArray.toString())
时应该得到的。使用resultArray[0]。键入可访问数组中第一个对象的属性
type
。谢谢:)现在可以使用了