0

我有这样的数据

[{"search":true,"hidden":true,"index":"xyz"},{"search":true,"hidden":true,"index":"pqr","address":{"city":"xxxx"}}]

我希望拆分数据得到:

  1. {"search":true,"hidden":true,"index":"xyz"}
  2. {"search":true,"hidden":true,"index":"pqr","address":{"city":"xxxx"}}

我的正则表达式很弱,请帮忙。

4

3 回答 3

0

如果您的数据是该格式的单个字符串,则很可能您有数据结构的JSON表示。您应该使用 json 库将其解析为哈希数组:

require 'json'
require 'pp'

s = '[{"search":true,"hidden":true,"index":"xyz"},{"search":true,"hidden":true,"index":"pqr","address":{"city":"xxxx"}}]'

a = JSON[s]
pp a

# => [{"search"=>true, "hidden"=>true, "index"=>"xyz"},
# =>  {"search"=>true, "hidden"=>true, "index"=>"pqr", "address"=>{"city"=>"xxxx"}}
于 2012-09-26T10:34:56.050 回答
0

尝试

collection = JSON.parse('[{"search":true,"hidden":true,"index":"xyz"},{"search":true,"hidden":true,"index":"pqr","address":{"city":"xxxx"}}]')
first = collection[0]
last = collection[-1]
于 2012-09-26T10:32:12.930 回答
0
arr = [{"search":true,"hidden":true,"index":"xyz"},{"search":true,"hidden":true,"index":"pqr","address":{"city":"xxxx"}}]

arr[0] #{"search":true,"hidden":true,"index":"xyz"}
arr[1] #{"search":true,"hidden":true,"index":"pqr","address":{"city":"xxxx"}} 
于 2012-09-26T10:32:53.070 回答