【问题标题】:rails unable to store html inside json fieldrails 无法在 json 字段中存储 html
【发布时间】:2020-10-01 20:51:24
【问题描述】:

我尝试创建 json 并将其存储在 json 字段中,但所有 html 标记都更改为某些代码

 @vs=@tvrs.vsrs.new()
 @vs.jsr = ({"desc": "<html>\n<body>\n<ul>\n  <li>Generate client.</li>\n  <li>In every month. 
             </li>\n  \n</ul>  \n\n\n\n</body>\n</html>"})
 @vs.save()

存储后显示:-

{"id":11,"jsr":{"desc":"\u003chtml\u003e\n\u003cbody\u003e\n\u003cul\u003e\n  \u003cli\u003eGenerate client.\u003c/li\u003e\n  \u003cli\u003eIn every month.\u003c/li\u003e\n  \n\u003c/ul\u003e  \n\n\n\n\u003c/body\u003e\n\u003c/html\u003e"},"tvr_id":1,"created_at":"2020-06-12T04:01:16.116Z","updated_at":"2020-06-12T04:01:16.116Z"}

预期输出:-

{"id":11,"jsr":{"desc": "<html>\n<body>\n<ul>\n  <li>Generate client.</li>\n  <li>In every month. 
             </li>\n  \n</ul>  \n\n\n\n</body>\n</html>"},"tvr_id":1,"created_at":"2020-06-12T04:01:16.116Z","updated_at":"2020-06-12T04:01:16.116Z"}

【问题讨论】:

    标签: ruby-on-rails json ruby postgresql ruby-on-rails-5


    【解决方案1】:

    你试过了吗:

    # let's assume you have a @vs instance variable which has an attribute jsr.
    JSON.parse(@vs.jsr)
    => {"desc": "<html>\n<body>\n<ul>\n  <li>Generate client.</li>\n  <li>In every month. </li>\n  \n</ul>  \n\n\n\n</body>\n</html>"}
    

    【讨论】:

    • 我使用了 jsonb 字段而不是 json 并且问题得到了解决。感谢您的回复和解决方案。 :)
    猜你喜欢
    • 1970-01-01
    • 2020-09-21
    • 2016-02-23
    • 2011-09-25
    • 2017-01-24
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多