{"realtime_start"=>"1776-07-04", "-6ren">
gpt4 book ai didi

ruby-on-rails - rails在 View 中美化json

转载 作者:行者123 更新时间:2023-12-04 07:08:04 24 4
gpt4 key购买 nike

我想在 View 中显示来自 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 放在我的 View 中:

    <%= @beautifiedJson %>

...风景未美化。当我查看页面源代码时,json 已格式化(正确的缩进和行),但是,在 View 中正确显示所需的 html 格式没有到位。

我的问题是我是否正确地使用了 rails JSON 方法,还是我需要另一种方法来完成格式化?

最佳答案

使用:

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

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

关于ruby-on-rails - rails在 View 中美化json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14426386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com