【发布时间】:2014-08-07 02:57:10
【问题描述】:
我在 Rails (3.2.19) 中有一个 Web 服务,它对 JSON 进行编码以供某些 iOS 或 Android 应用程序读取。
json 可能包含任何字符,但每次我使用 4 字节 UTF8 字符(例如 ????)时它似乎都会失败:它会生成 \uf4a9 aka 。
在 Rails 控制台中,字符正确显示,但是当我在 iOS 中使用 AFNetworking 或 HTTParty 检索答案时,它失败了。
这是我检索错误 JSON 的代码示例:
puts HTTParty.post( 'http://0.0.0.0:3000/login',
:body => { :login => 'antoine',
:password => 'thisisnotmypassword',
}.to_json,
:headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json' })
对 JSON 进行编码:
format.json { render json: json_reponse } #json_response is a Hash.
发送其他 UTF-8 字符效果很好,例如:ضصيتحضصتحخـ
【问题讨论】:
标签: ruby-on-rails json utf-8 afnetworking httparty