简体   繁体   English

如何将字符串转换为数组数组?

[英]How do I convert a string to an array of arrays?

I have a string with an array of arrays inside: 我有一个包含数组数组的字符串:

"[[1, 2], [3, 4], [5, 6]]"

Can I convert this to the array of arrays, without using eval or a regular expression, gsub , etc.? 我可以将其转换为数组数组,而不使用eval或正则表达式, gsub等吗?

Can I make turn it into: 我可以把它变成:

[[1, 2], [3, 4], [5, 6]]

How about the following? 以下怎么样?

require 'json'
arr = JSON.parse("[[1, 2], [3, 4], [5, 6]]") # => [[1, 2], [3, 4], [5, 6]]
arr[0] # => [1, 2]

The same can be done using Ruby standard libaray documentation - YAML : 使用Ruby标准的libaray文档可以完成同样的事情YAML

require 'yaml'

YAML.load("[[1, 2], [3, 4], [5, 6]]")
 # => [[1, 2], [3, 4], [5, 6]]  

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

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