Ruby-on-rails 如何将哈希打印到 Rails 视图中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17289399/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:13:02  来源:igfitidea点击:

How do I pretty print a hash to a Rails view?

ruby-on-railsrubyjsonhashpretty-print

提问by chintanparikh

I have something like:

我有类似的东西:

{"a":"1","b":"2","c":"3","asefw":"dfsef"}

I want to print it out in a view. What's the best way to do that?

我想在视图中打印出来。这样做的最佳方法是什么?

I tried parsing it as a JSON object and using JSON.stringify, but it seems to mess up indentation.

我尝试将其解析为 JSON 对象并使用JSON.stringify,但它似乎弄乱了缩进。

Any advice? I don't mind a JavaScript solution.

有什么建议吗?我不介意 JavaScript 解决方案。

回答by the Tin Man

How about:

怎么样:

require 'json'

hash = JSON['{"a":"1","b":"2","c":"3","asefw":"dfsef"}']
puts JSON.pretty_generate(hash)

Which outputs:

哪些输出:

{
  "a": "1",
  "b": "2",
  "c": "3",
  "asefw": "dfsef"
}

JSON.pretty_generateis more of a debugging tool than something I'd rely on when actually generating JSON to be sent to a browser. The "pretty" aspect also means "bloated" and "slower" because of the added whitespace, but it is good for diagnosing and comprehending what is in the structure so it might work well for your needs.

JSON.pretty_generate在实际生成要发送到浏览器的 JSON 时,它更像是一种调试工具,而不是我所依赖的工具。由于添加了空格,“漂亮”方面也意味着“臃肿”和“较慢”,但它有助于诊断和理解结构中的内容,因此它可能会很好地满足您的需求。

One thing to remember is that HTML, when rendered by a browser, has whitespace gobbled up, so whitespace runs disappear. To avoid that you have to wrap the JSON output in a <pre>block to preserve the whitespace and line-breaks. Something like this should work:

需要记住的一件事是,当 HTML 由浏览器呈现时,空白被吞噬了,因此空白运行消失了。为避免这种情况,您必须将 JSON 输出包装在一个<pre>块中以保留空格和换行符。这样的事情应该工作:

<pre>
{
  "a": "1",
  "b": "2",
  "c": "3",
  "asefw": "dfsef"
}
</pre>

回答by Amol Pujari

<%= raw JSON.pretty_generate(hash).gsub(" ","&nbsp;") %>

回答by Phrogz

If you (like I) find that the pretty_generateoption built into Ruby's JSON library is not "pretty" enough, I recommend my own NeatJSONgem for your formatting.

如果您(像我一样)发现pretty_generateRuby 的 JSON 库中内置的选项不够“漂亮”,我建议NeatJSON您使用我自己的gem 进行格式化。

To use it gem install neatjsonand then use JSON.neat_generateinstead of JSON.pretty_generate.

要使用它gem install neatjson,然后使用JSON.neat_generate代替JSON.pretty_generate.

Like Ruby's ppit will keep objects and arrays on one line when they fit, but wrap to multiple as needed. For example:

像 Ruby 一样,pp当对象和数组适合时,它会将对象和数组保留在一行上,但根据需要包装为多个。例如:

{
  "navigation.createroute.poi":[
    {"text":"Lay in a course to the Hilton","params":{"poi":"Hilton"}},
    {"text":"Take me to the airport","params":{"poi":"airport"}},
    {"text":"Let's go to IHOP","params":{"poi":"IHOP"}},
    {"text":"Show me how to get to The Med","params":{"poi":"The Med"}},
    {"text":"Create a route to Arby's","params":{"poi":"Arby's"}},
    {
      "text":"Go to the Hilton by the Airport",
      "params":{"poi":"Hilton","location":"Airport"}
    },
    {
      "text":"Take me to the Fry's in Fresno",
      "params":{"poi":"Fry's","location":"Fresno"}
    }
  ],
  "navigation.eta":[
    {"text":"When will we get there?"},
    {"text":"When will I arrive?"},
    {"text":"What time will I get to the destination?"},
    {"text":"What time will I reach the destination?"},
    {"text":"What time will it be when I arrive?"}
  ]
}

It also supports a variety of formatting optionsto further customize your output. For example, how many spaces before/after colons? Before/after commas? Inside the brackets of arrays and objects? Do you want to sort the keys of your object? Do you want the colons to all be lined up?

它还支持各种格式选项以进一步自定义您的输出。例如,冒号前后有多少个空格?逗号前/后?在数组和对象的括号内?您想对对象的键进行排序吗?你想让所有的冒号都排成一行吗?

For example, using your example Hash, you can get these different outputs, depending on what you want:

例如,使用您的示例哈希,您可以获得这些不同的输出,具体取决于您想要的:

// JSON.neat_generate(o, wrap:true)
{
  "a":"1",
  "b":"2",
  "c":"3",
  "asefw":"dfsef"
}

// JSON.neat_generate o, wrap:true, aligned:true
{
  "a"    :"1",
  "b"    :"2",
  "c"    :"3",
  "asefw":"dfsef"
}

// JSON.neat_generate o, wrap:true, aligned:true, around_colon:1
{
  "a"     : "1",
  "b"     : "2",
  "c"     : "3",
  "asefw" : "dfsef"
}

回答by hophacker

irb(main)> puts queried_object.pretty_inspect

irb(main)> puts queried_object.pretty_inspect

回答by Daniel Arenas

You can try the gem awesome_printworks very well, and in your view write

你可以试试 gem awesome_print效果很好,在你看来写

<%= ap(your_hash, plain: true, indent: 0).html_safe %>

also, you can change the values for config the styles to hash view

此外,您可以将配置样式的值更改为散列视图

回答by Belkacem REBBOUH

The given response is works fine, but if you want to have prettier and more custom pretty hash, use awesome_print

给定的响应工作正常,但如果您想拥有更漂亮和更自定义的漂亮哈希,请使用awesome_print

require 'awesome_print'
hash = JSON['{"a":"1","b":"2","c":"3","asefw":"dfsef"}']
ap hash 

Cheers!

干杯!