Convert JSON String to JSON Array in rails?

前端 未结 3 848
谎友^
谎友^ 2021-02-01 14:58

I have a JSON string in rails as shown below:

[{\"content\":\"1D\",\"createdTime\":\"09-06-2011 00:59\"},{\"content\":\"2D\",\"createdtime\":\"09-06-2011 08:00         


        
3条回答
  •  佛祖请我去吃肉
    2021-02-01 15:42

    If JavaScript code is internal then you can do this:

    
    

    Otherwise:

    create hidden textarea and set @hives.html_safe as its value now you can get it in JavaScript as element's value as shown below:

    In html.erb file

    <%= text_area_tag :hives_yearly_temp, @hives.html_safe, { style: "display: none;"} %>
    

    In js file

    var hives = JSON.parse( $('#hives_yearly_temp').val() );
    

    To run loop

    for(key in hives) {
      alert( hives[key] );
    }
    

提交回复
热议问题