Ruby JSON.pretty_generate ... 很不漂亮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2567670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby JSON.pretty_generate ... is pretty unpretty
提问by Amy
I can't seem to get JSON.pretty_generate()to actually generate pretty output in Rails.
我似乎无法JSON.pretty_generate()在 Rails 中实际生成漂亮的输出。
I'm using Rails 2.3.5 and it seems to automatically load the JSON gem. Awesome. While using script/consolethis does indeed produce JSON:
我正在使用 Rails 2.3.5,它似乎会自动加载 JSON gem。惊人的。虽然使用script/console它确实会产生 JSON:
some_data = {'foo' => 1, 'bar' => 20, 'cow' => [1, 2, 3, 4], 'moo' => {'dog' => 'woof', 'cat' => 'meow'}}
some_data.to_json
=> "{\"cow\":[1,2,3,4],\"moo\":{\"cat\":\"meow\",\"dog\":\"woof\"},\"foo\":1,\"bar\":20}"
But this doesn't produce pretty output:
但这不会产生漂亮的输出:
JSON.pretty_generate(some_data)
=> "{\"cow\":[1,2,3,4],\"moo\":{\"cat\":\"meow\",\"dog\":\"woof\"},\"foo\":1,\"bar\":20}"
The only way I've found to generate it is to use irband to load the "Pure" version:
我发现生成它的唯一方法是使用irb并加载“纯”版本:
require 'rubygems'
require 'json/pure'
some_data = {'foo' => 1, 'bar' => 20, 'cow' => [1, 2, 3, 4], 'moo' => {'dog' => 'woof', 'cat' => 'meow'}}
JSON.pretty_generate(some_data)
=> "{\n \"cow\": [\n 1,\n 2,\n 3,\n 4\n ],\n \"moo\": {\n \"cat\": \"meow\",\n \"dog\": \"woof\"\n },\n \"foo\": 1,\n \"bar\": 20\n}"
BUT, what I really want is Rails to produce this. Does anyone have any tips why I can't get the generator in Rails to work correctly?
但是,我真正想要的是 Rails 来生产这个。有没有人有任何提示为什么我不能让 Rails 中的生成器正常工作?
Thanks!
谢谢!
回答by Geoff Petrie
To generate pretty JSON output it appears that you're only missing a putscall.
要生成漂亮的 JSON 输出,您似乎只是错过了一个puts电话。
The data:
数据:
some_data = {'foo' => 1, 'bar' => 20, 'cow' => [1, 2, 3, 4], 'moo' => {'dog' => 'woof', 'cat' => 'meow'}}
Calling only JSON.pretty_generate:
仅调用JSON.pretty_generate:
> JSON.pretty_generate some_data
=> "{\n \"foo\": 1,\n \"bar\": 20,\n \"cow\": [\n 1,\n 2,\n 3,\n 4\n ],\n \"moo\": {\n \"dog\": \"woof\",\n \"cat\": \"meow\"\n }\n}"
Adding a putsinto the mix:
puts在混合中添加一个:
> puts JSON.pretty_generate some_data
{
"foo": 1,
"bar": 20,
"cow": [
1,
2,
3,
4
],
"moo": {
"dog": "woof",
"cat": "meow"
}
}
回答by Samuel
I use Rails 2.3.8 and installed the JSON gem (gem install json). JSON.pretty_generatenow does nicely in script/console:
我使用 Rails 2.3.8 并安装了 JSON gem ( gem install json)。JSON.pretty_generate现在在脚本/控制台中做得很好:
>> some_data = {'foo' => 1, 'bar' => 20, 'cow' => [1, 2, 3, 4], 'moo' => {'dog' => 'woof', 'cat' => 'meow'}}
=> {"cow"=>[1, 2, 3, 4], "moo"=>{"cat"=>"meow", "dog"=>"woof"}, "foo"=>1, "bar"=>20}
>> JSON.pretty_generate(some_data)
=> "{\n \"cow\": [\n 1,\n 2,\n 3,\n 4\n ],\n \"moo\": {\n \"cat\": \"meow\",\n \"dog\": \"woof\"\n },\n \"foo\": 1,\n \"bar\": 20\n}"

