Skip to content Skip to sidebar Skip to footer

How Can I Use Javascript To Parse Ruby Json String

I'm trying to get the JSON object from a JSON outputted string from a Rails app. Currently in JavaScript I'm doing: data = '<%= @chromosomes.html_safe %>'; However, since th

Solution 1:

Why don't you do:

data = <%= @chromosomes.html_safe %>;

Sidenote:

I hope you do something like:

@chromosomes = [{ name: "YHet", organism_id: "foo" }].to_json

Solution 2:

If you are using jQuery you can do the following

var data = jQuery.parseJSON('[{"name":"YHet","organism_id":"4ea9b90e859723d3f7000037"}]');

Solution 3:

Solution 4:

Use JSON object that is included in most of browsers or if you are using jQuery use $.jsonParse method that try to use JSON object if defined otherwise parse using eval or some safer way.

Solution 5:

On controller

@my_var = MyObj.find_by_id(4).to_json

On page in haml way.

var my_json = $.parseJSON("#{j @my_var}"); //used JQuery to parser JSON string

Post a Comment for "How Can I Use Javascript To Parse Ruby Json String"