我想在视图中显示一些来自api的json“美化”以用于调试目的。例如,而不是显示:
{"observations"=>{"realtime_start"=>"1776-07-04", "realtime_end"=>"9999-12-31", "observation_start"=>"1776-07-04", "observation_end"=>"9999-12-31", "units"=>"lin", "output_type"=>"1", "file_type"=>"xml", "order_by"=>"observation_date", "sort_order"=>"desc", "count"=>"2541", "offset"=>"0", "limit"=>"10", "observation"=>[{"realtime_start"=>"2013-01-10", "realtime_end"=>"9999-12-31", "date"=>"2012-12-01", "value"=>"1458.750"}, {"realtime_start"=>"2012-12-13", "realtime_end"=>"2012-12-19", "date"=>"2012-11-01", "value"=>"1435.307"}, {"realtime_start"=>"2012-12-20", "realtime_end"=>"2013-01-09", "date"=>"2012-11-01", "value"=>"1435.304"}, {"realtime_start"=>"2013-01-10", "realtime_end"=>"9999-12-31", "date"=>"2012-11-01", "value"=>"1435.303"}, {"realtime_start"=>"2012-11-01", "realtime_end"=>"2012-11-07", "date"=>"2012-10-01", "value"=>"1418.277"}, {"realtime_start"=>"2012-11-08", "realtime_end"=>"2012-11-14", "date"=>"2012-10-01", "value"=>"1418.286"}, {"realtime_start"=>"2012-11-15", "realtime_end"=>"2012-11-22", "date"=>"2012-10-01", "value"=>"1418.285"}, {"realtime_start"=>"2012-11-23", "realtime_end"=>"2012-11-28", "date"=>"2012-10-01", "value"=>"1418.284"}, {"realtime_start"=>"2012-11-29", "realtime_end"=>"9999-12-31", "date"=>"2012-10-01", "value"=>"1418.274"}, {"realtime_start"=>"2012-10-04", "realtime_end"=>"2012-10-10", "date"=>"2012-09-01", "value"=>"1409.636"}]}}
...我想显示:
{
"observations": {
"realtime_start": "1776-07-04",
"realtime_end": "9999-12-31",
"observation_start": "1776-07-04",
"observation_end": "9999-12-31",
"units": "lin",
"output_type": "1",
"file_type": "xml",
"order_by": "observation_date",
"sort_order": "desc",
"count": "2541",
"offset": "0",
"limit": "10",
"observation": [{
"realtime_start": "2013-01-10",
"realtime_end": "9999-12-31",
"date": "2012-12-01",
"value": "1458.750"
}, {
"realtime_start": "2012-12-13",
"realtime_end": "2012-12-19",
"date": "2012-11-01",
"value": "1435.307"
}, {
"realtime_start": "2012-12-20",
"realtime_end": "2013-01-09",
"date": "2012-11-01",
"value": "1435.304"
}, {
"realtime_start": "2013-01-10",
"realtime_end": "9999-12-31",
"date": "2012-11-01",
"value": "1435.303"
}, {
"realtime_start": "2012-11-01",
"realtime_end": "2012-11-07",
"date": "2012-10-01",
"value": "1418.277"
}, {
"realtime_start": "2012-11-08",
"realtime_end": "2012-11-14",
"date": "2012-10-01",
"value": "1418.286"
}, {
"realtime_start": "2012-11-15",
"realtime_end": "2012-11-22",
"date": "2012-10-01",
"value": "1418.285"
}, {
"realtime_start": "2012-11-23",
"realtime_end": "2012-11-28",
"date": "2012-10-01",
"value": "1418.284"
}, {
"realtime_start": "2012-11-29",
"realtime_end": "9999-12-31",
"date": "2012-10-01",
"value": "1418.274"
}, {
"realtime_start": "2012-10-04",
"realtime_end": "2012-10-10",
"date": "2012-09-01",
"value": "1409.636"
}]
}
}
当我分配变量@beautifiedJson = JSON.pretty_generate(parseHolder)时,将parseHolder作为json对象,然后将@beautifiedJson放在我的视图中:
<%= @beautifiedJson %>
......视图没有被美化。当我查看页面源时,json被格式化(正确的缩进和行),但是,在视图中正确显示的必要html格式不适用。
我的问题是我是否正确使用rails JSON方法,还是需要其他方法来完成格式化?
答案 0 :(得分:5)
使用:
<pre>
<%= @beautifiedJson %>
</pre>
如果您查看原始代码生成的页面源,您会看到它已经被美化,但浏览器忽略了JSON.pretty_generate(parseHolder)
添加的换行符和标签页
答案 1 :(得分:3)
所以答案就是:
<pre><%= @beautifiedJson %></pre>