我如何使用JavaScript来解析Ruby JSON字符串 [英] How can I use JavaScript to parse Ruby JSON string
本文介绍了我如何使用JavaScript来解析Ruby JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从Rails应用程序的JSON输出字符串中获取JSON对象。目前我正在使用JavaScript:
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 %>";
但是,由于JSON对象的开头和结尾都有引号,因此它不会被渲染作为JSON对象。相反,它正在做类似
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?
推荐答案
你为什么不这样做:
data = <%= @chromosomes.html_safe %>;
旁注:
我希望你做类似于:
@chromosomes = [{ name: "YHet", organism_id: "foo" }].to_json
这篇关于我如何使用JavaScript来解析Ruby JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文