使用Javascript解析Ruby哈希文字 [英] Parsing Ruby hash literal using Javascript

查看:107
本文介绍了使用Javascript解析Ruby哈希文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Web表单中获取原始客户端文本并将其作为JSON发送回Tomcat servlet。由于遗留原因,此输入可能被格式化为Ruby哈希。我也不能强迫我的客户将他们现有的Ruby格式化输入转换为JSON。我可以编写一个自定义解析器,但我想查看是否存在一个基于JavaScript的解决方案,以便确定一个文本块是否为Ruby哈希,如果是,则将其转换为JSON。

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.

推荐答案

JSON是一个Ruby标准库。你只需要它:

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

require 'json'

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

Ref: 生成JSON

这篇关于使用Javascript解析Ruby哈希文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆