2

我想在视图中显示一些“美化”的 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 方法还是需要另一种方法来完成格式化?

4

2 回答 2

6

采用:

<pre>
    <%= @beautifiedJson %>
</pre>

如果您查看原始代码生成的页面源代码,您会发现它已被美化,但浏览器忽略了JSON.pretty_generate(parseHolder)正在添加的换行符和制表符。

于 2013-01-20T16:57:08.163 回答
3

所以答案只是:

<pre><%= @beautifiedJson %></pre>
于 2013-01-20T16:49:19.407 回答