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 there are quotes at the beginning and end of the JSON object, it is not being rendered as a JSON object. Instead it is doing something like data = "[{"name":"YHet","organism_id":"4ea9b90e859723d3f7000037"}]" Is there a way that I can remove the beginning and end quotes so that the object is treated as an array instead of a string?
-
Answer:
Why don't you do: data = <%= @chromosomes.html_safe %>; Sidenote: I hope you do something like: @chromosomes = [{ name: "YHet", organism_id: "foo" }].to_json
Phillip Whisenhunt at Stack Overflow Visit the source
Other answers
Use eval: var dataObject = eval('(' + dataString + ')');
rottz
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.
Hauleth
If you are using jQuery you can do the following var data = jQuery.parseJSON('[{"name":"YHet","organism_id":"4ea9b90e859723d3f7000037"}]');
Two Seeds
Related Q & A:
- How can I use SSL with django?Best solution by Stack Overflow
- How can I use XMP in Sharepoint metadata?Best solution by Stack Overflow
- How can I use mongo-sync?Best solution by github.com
- How can I use real time social data from Datasift and perform real time analytics on it?Best solution by Quora
- How can I echo characters before and after a string?Best solution by stackoverflow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.