Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.?

Can I make turn it into:

[[1, 2], [3, 4], [5, 6]]
like image 724
tbone Avatar asked Jun 24 '13 00:06

tbone


1 Answers

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]
like image 55
vee Avatar answered Sep 27 '22 17:09

vee