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

試す

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 に答える
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 に答える