0

想象一下,我有这个:

[ 
  {"id":1, "name":"bla", "other":"aaaa"},
  {"id":2, "name":"ble", "other":"eeee"},
  {"id":3, "name":"bli", "other":"iiii"},
]

我想在我的 someview.html.erb 中看到

Id:1
name:Bla
other:aaaa

Id:2
name:Ble
other:eee

...

当我说 JSON 时,我的意思是我不知道对象属性。我只想自动格式化 JSON。

4

1 回答 1

1

尝试这个:

require 'JSON'
require 'yaml'

x = '[
  {"id":1, "name":"bla", "other":"aaaa"},
  {"id":2, "name":"ble", "other":"eeee"},
  {"id":3, "name":"bli", "other":"iiii"}
]'

puts JSON.parse(x).to_yaml
于 2012-08-11T05:22:16.437 回答