简体   繁体   English

JSON.parse如何工作?

[英]How does JSON.parse worked?

console.log(this.item_images)
console.log(JSON.parse("["+this.item_images+"]"))

I want to turn this.item_images into an array and loop over it, why can't I parse it? 我想将this.item_images转换成数组并循环遍历,为什么我不能解析它? Below is the result in the console of above code. 下面是上面代码的控制台中的结果。 Note : I console.log(typeof this.item_images) it's a string. 注意:我console.log(typeof this.item_images)是一个字符串。

在此处输入图片说明

this.item_images seems to be an array with only one element: the string '038...6aa' this.item_images似乎是一个只有一个元素的数组:字符串'038 ... 6aa'

So I guess you may want to split the string into another array. 因此,我猜您可能想将字符串拆分为另一个数组。

var arr = this.item_images[0].split(',');

Then you can loop the array arr . 然后,您可以循环数组arr

Update: If this.item_images is the string, use: 更新:如果this.item_images是字符串,请使用:

var arr = this.item_images.split(',');

instead. 代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM