我有类似的东西:
{"a":"1","b":"2","c":"3","asefw":"dfsef"}
我想在视图中打印出来。最好的方法是什么?
我尝试将其解析为JSON对象并使用JSON.stringify
,但它似乎搞乱了缩进。
有什么建议吗?我不介意JavaScript解决方案。
答案 0 :(得分:17)
怎么样:
require 'json'
hash = JSON['{"a":"1","b":"2","c":"3","asefw":"dfsef"}']
puts JSON.pretty_generate(hash)
哪个输出:
{
"a": "1",
"b": "2",
"c": "3",
"asefw": "dfsef"
}
JSON.pretty_generate
更像是一种调试工具,而不是我实际生成要发送到浏览器的JSON时依赖的东西。由于增加了空白,“漂亮”方面也意味着“臃肿”和“慢”,但它有助于诊断和理解结构中的内容,因此它可以很好地满足您的需求。
要记住的一件事是,HTML在浏览器呈现时会占用空格,因此空格会消失。为了避免这种情况,您必须将JSON输出包装在<pre>
块中以保留空白和换行符。这样的事情应该有效:
<pre>
{
"a": "1",
"b": "2",
"c": "3",
"asefw": "dfsef"
}
</pre>
答案 1 :(得分:3)
<%= raw JSON.pretty_generate(hash).gsub(" "," ") %>
答案 2 :(得分:3)
如果你(像我一样)发现Ruby的JSON库中内置的pretty_generate
选项不够“漂亮”,我推荐自己的NeatJSON
gem用于格式化。
要使用gem install neatjson
,然后使用JSON.neat_generate
代替JSON.pretty_generate
。
与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?"}
]
}
它还支持各种formatting options以进一步自定义您的输出。例如,冒号之前/之后有多少个空格?逗号之前/之后?在数组和对象的括号内?你想对对象的键进行排序吗?你想要将冒号排成一行吗?
例如,使用您的示例Hash,您可以根据需要获得这些不同的输出:
// 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"
}
答案 3 :(得分:3)
IRB(主)GT; puts queried_object.pretty_inspect
答案 4 :(得分:2)
给定的响应工作正常,但如果您想要更漂亮且更自定义的漂亮哈希,请使用awesome_print
require 'awesome_print'
hash = JSON['{"a":"1","b":"2","c":"3","asefw":"dfsef"}']
ap hash
干杯!
答案 5 :(得分:1)
您可以尝试使用gem awesome_print,并在您的视图中编写
<%= ap(your_hash, plain: true, indent: 0).html_safe %>
另外,您可以将样式配置的值更改为哈希视图
答案 6 :(得分:0)
Pretty Print Hash
使用纯 Ruby(无宝石)
我遇到了这个线程,试图为自己解决这个问题。
我有一个很大的 Hash
,我想让它变得漂亮,但我需要使用 ruby 哈希表示法而不是 JSON。
这是代码+示例
symbol:
等价物替换所有 JSON 键puts JSON.pretty_generate(result)
.gsub(/(?:\"|\')(?<key>[^"]*)(?:\"|\')(?=:)(?:\:)/) { |_|
"#{Regexp.last_match(:key)}:"
}
示例 JSON
{
"extensions": {
"heading": "extensions",
"take": "all",
"array_columns": [
"name"
]
},
"tables": {
"heading": "tables",
"take": "all",
"array_columns": [
"name"
]
},
"foreign_keys": {
"heading": "foreign_keys",
"take": "all",
"array_columns": [
"name"
]
},
"all_indexes": {
"heading": "all_indexes",
"take": "all",
"array_columns": [
"name"
]
},
"keys": {
"heading": "keys",
"take": "all",
"array_columns": [
"name"
]
}
}
示例 Ruby 哈希
{
extensions: {
heading: "extensions",
take: "all",
array_columns: [
"name"
]
},
tables: {
heading: "tables",
take: "all",
array_columns: [
"name"
]
},
foreign_keys: {
heading: "foreign_keys",
take: "all",
array_columns: [
"name"
]
},
all_indexes: {
heading: "all_indexes",
take: "all",
array_columns: [
"name"
]
},
keys: {
heading: "keys",
take: "all",
array_columns: [
"name"
]
}
}