Ruby on Rails 3.1 中的 toJSON 和 UTF8 字符串编码错误
我在使用 to_json 导出 UTF8 字符串时遇到问题。
问题是,当我这样做时:
ruby-1.9.2-p290 :005 > "anche il più remoto".encoding
=> #<Encoding:UTF-8>
ruby-1.9.2-p290 :006 > {:text => "anche il più remoto"}.to_json
=> "{\"text\":\"anche il pi\\u00f9 remoto\"}"
{:text => "anche il più remoto"}.to_json.encoding
=> #<Encoding:US-ASCII>
ruby-1.9.2-p290 :009 >
如何跳过更改编码?我想将 UTF8 字符串返回给客户端,这样他们就无需对其进行解码。
I've got problem with UTF8 string exported using to_json.
The problem is that when I do this:
ruby-1.9.2-p290 :005 > "anche il più remoto".encoding
=> #<Encoding:UTF-8>
ruby-1.9.2-p290 :006 > {:text => "anche il più remoto"}.to_json
=> "{\"text\":\"anche il pi\\u00f9 remoto\"}"
{:text => "anche il più remoto"}.to_json.encoding
=> #<Encoding:US-ASCII>
ruby-1.9.2-p290 :009 >
How can i skip changing the encoding? I want to return UTF8 string to the clients so they don't have decode it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你使用的是 json 还是 ruby-json gem?从此处来看,ruby-json 似乎存在问题。
尝试
Are you using json or ruby-json gem? From here, it's looking like ruby-json is having problems.
Try