Parsing Ruby hash literal using Javascript

房东的猫 提交于 2020-01-06 01:56:51

问题


I need to take raw client text in a web form and send it back to a Tomcat servlet as JSON. For legacy reasons, this input may be formatted as a Ruby hash. I also cannot force my clients to convert their existing Ruby formatted inputs over to JSON. I could write a custom parser, but I wanted to see if a JavaScript based solution existed that would allow me to determine if a blob of text is a Ruby hash and, if so, convert it into JSON.


回答1:


JSON is a Ruby standard library. You've to just require it:

require 'json'

data = {:hello => "goodbye"}
p data.to_json #=> "{\"hello\":\"goodbye\"}"

Ref: Generating JSON



来源:https://stackoverflow.com/questions/35109343/parsing-ruby-hash-literal-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!