简体   繁体   中英

Ruby hash to json

I have a ruby hash in my contoller

@my_hash = {
        name: 'Tokyo',
        data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
        }

When I use this in my js file

.
.
.
series: <%= @my_hash.to_json %>  

I am getting Uncaught SyntaxError: Unexpected token & The json object is like

&quot;{\&quot;name\&quot;:\&quot;Tokyo\&quot;,\&quot;data\&quot;:[7.0,6.9,9.5,14.5,18.2,21.5,25.2,26.5,23.3,18.3,13.9,9.6]}&quot;

how to get rid of &quot; when I use it in my js file? Any help much appreciated. I have been searching and found that this is a common issue.

If you convert hash into json then you can use .to_json it will give you hash with "/" , in your case you can not use .to_json ,Instead of this you may use .as_josn ,It will convert your hash without "/" . like:

<%= @my_hash.as_json %>

你也可以用生的

<%= raw @my_hash.to_json %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM