我想转换这个字符串:
'[
['Row1 of first array', 'Row2 of first array'],
['Row1 of 2nd array', 'Row2 of 2nd array']
]'转换为包含三个一维数组和两个项的数组。
我的预期输出是一个包含两个元素的数组:
每个数组里面都有两个元素。
在Jquery中有进行这种转换的吗?
发布于 2012-11-19 14:06:33
这不是一个有效的字符串--你在单引号里面嵌套单引号。但是,如果在内部使用双引号将字符串转换为一个字符串:
str = '[ ["Row1 of first array", "Row2 of first array"], ["Row1 of 2nd array", "Row2 of 2nd array"] ]'然后您可以简单地将其解析为一个JSON对象:
arr = $.parseJSON(str); // returns a two-dimensional array这比使用eval要安全得多,只有当您确切地知道字符串中的内容时,才应该这样做,即使这样,这也是一个懒惰的开发人员的迹象。当使用parseJSON时,您知道当您使用eval时,您将得到一个对象或一个数组--任何事情都可能发生。
发布于 2012-11-19 14:07:07
我想伊娃会成功的:
var str = eval("[['Row1 of first array', 'Row2 of first array'],['Row1 of 2nd array', 'Row2 of 2nd array']]");
console.log(str);
https://stackoverflow.com/questions/13455327
复制相似问题