Ruby on Rails emoji表情通过json返回
2016-03-25 00:00
351 查看
摘要: 解决emoji通过json返回不显示
原因:
大概意思是:emoji表情是5位字符 而to_json/as_json最多支持4个字符,导致返回的emoji不完整,客户端就不会识别,导致乱码。
解决方法:
在config/initializers文件夹下新建patches.rb 文件:
config/initializers/patches.rb文件内容:
或者
参考链接:
http://stackoverflow.com/questions/5123993/json-encoding-wrongly-escaped-rails-3-ruby-1-9-2/8339255#8339255
http://stackoverflow.com/questions/683989/how-do-you-deal-with-the-conflict-between-activesupportjson-and-the-json-gem/3285570#3285570
原因:
大概意思是:emoji表情是5位字符 而to_json/as_json最多支持4个字符,导致返回的emoji不完整,客户端就不会识别,导致乱码。
解决方法:
在config/initializers文件夹下新建patches.rb 文件:
config/initializers/patches.rb文件内容:
module ActiveSupport::JSON::Encoding class << self def escape(string) if string.respond_to?(:force_encoding) string = string.encode(::Encoding::UTF_8, :undef => :replace).force_encoding(::Encoding::BINARY) end json = string.gsub(escape_regex) { |s| ESCAPED_CHARS[s] } json = %("#{json}") json.force_encoding(::Encoding::UTF_8) if json.respond_to?(:force_encoding) json end end end
或者
module ActiveSupport module JSON module Encoding class << self def escape(string) ::JSON.generate([string])[1..-2] end end end end end
参考链接:
http://stackoverflow.com/questions/5123993/json-encoding-wrongly-escaped-rails-3-ruby-1-9-2/8339255#8339255
http://stackoverflow.com/questions/683989/how-do-you-deal-with-the-conflict-between-activesupportjson-and-the-json-gem/3285570#3285570
相关文章推荐
- XML 与 JSON 优劣对比
- 红宝石(Ruby)史话
- VBA将excel数据表生成JSON文件
- On Error Resume Next 语句
- Ruby简明教程之循环语句介绍
- ruby 一些简单的例子
- Ruby on Rails在Ping ++ 平台实现支付
- Ruby 之 class 中的 private、 protected、public
- Ruby中操作文件的方法介绍
- Ruby中的集合编写指南
- CentOS 6.3下编译安装Ruby 2.0笔记
- Ruby基本的环境变量设置以及常用解释器命令介绍
- Ruby编程中关于中断和返回的用法教程
- Ruby中的异常处理代码编写示例
- Ruby中的变量学习总结
- 几个加速Ruby on Rails的编程技巧
- Ruby配置rspec和RestClient来检测服务器
- 分析Cache 在 Ruby China 里面的应用情况