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解决方案。
6 个解决方案
#1
15
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_generate
is 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>
{
"a": "1",
"b": "2",
"c": "3",
"asefw": "dfsef"
}
</pre>
#2
3
<%= raw JSON.pretty_generate(hash).gsub(" "," ") %>
#3
2
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!
干杯!
#4
2
If you (like I) find that the pretty_generate
option built into Ruby's JSON library is not "pretty" enough, I recommend my own NeatJSON
gem for your formatting.
如果您(像我一样)发现内置在Ruby的JSON库中的pretty_generate选项不够“漂亮”,我建议您使用我自己的tidy JSON gem进行格式化。
To use it gem install neatjson
and then use JSON.neat_generate
instead of JSON.pretty_generate
.
要使用它,请安装clean JSON,然后使用JSON。neat_generate代替JSON.pretty_generate。
Like Ruby's pp
it 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 options to 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"
}
#5
2
irb(main)> puts queried_object.pretty_inspect
irb >把queried_object.pretty_inspect(主要)
#6
0
You can try the gem awesome_print works 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
此外,还可以将配置样式的值更改为散列视图
#1
15
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_generate
is 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>
{
"a": "1",
"b": "2",
"c": "3",
"asefw": "dfsef"
}
</pre>
#2
3
<%= raw JSON.pretty_generate(hash).gsub(" "," ") %>
#3
2
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!
干杯!
#4
2
If you (like I) find that the pretty_generate
option built into Ruby's JSON library is not "pretty" enough, I recommend my own NeatJSON
gem for your formatting.
如果您(像我一样)发现内置在Ruby的JSON库中的pretty_generate选项不够“漂亮”,我建议您使用我自己的tidy JSON gem进行格式化。
To use it gem install neatjson
and then use JSON.neat_generate
instead of JSON.pretty_generate
.
要使用它,请安装clean JSON,然后使用JSON。neat_generate代替JSON.pretty_generate。
Like Ruby's pp
it 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 options to 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"
}
#5
2
irb(main)> puts queried_object.pretty_inspect
irb >把queried_object.pretty_inspect(主要)
#6
0
You can try the gem awesome_print works 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
此外,还可以将配置样式的值更改为散列视图