Rails UTF-8响应 [英] Rails UTF-8 response

查看:128
本文介绍了Rails UTF-8响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Ruby 1.9.3上运行的Rails 3.2应用程序,它返回存储在MongoDB数据库中的JSON数据。数据似乎正确存储在mongo中,例如(查看名称属性):

  {_id:ObjectId(4f986cbe4c8086fdc9000002),created_at:ISODate 2012-04-25T21:31:45.474Z),updated_at:ISODate(2012-04-26T22:07:23.901Z),creator_id:ObjectId(4f6b4d3c4c80864381000001),updater_id ,name:Trädgår'n,sort:tradgarn,address:NyaAllén11,coordinates:[11.9764791,57.7045625],phone:46031102080,url :http://www.profilrestauranger.se/tradgarn/,user_ids:[]} 

但是当我发出返回此记录的请求时,我会收到这样的东西(现在看看name属性):

   

任何感兴趣的回复标题:

  HTTP / 1.1 200 OK 
服务器:nginx / 1.2.0
日期:星期四,2012年4月26日22:41:13 GMT
内容类型:application / json; charset = utf-8
内容长度:909
连接:keep-alive
状态:200 OK
访问控制允许原点:*
访问 - 控制允许 - 方法:POST,GET,OPTIONS
访问控制 - 最大年龄:1000
访问控制 - 允许标题:*,x请求 - 与
X-UA - 兼容:IE = Edge
ETag:d2a95f06bec10d8087c3188280292d3c
缓存控制:max-age = 0,private,must-revalidate
X-Request-Id:fdd042568195df279e59affe45bdcd37
X - 运行时:0.037134

我似乎无法弄清楚编码为什么或在哪里弄乱了?是什么赋予了?帮帮我? :)

解决方案

这个问题确实是JSON UTF-8编码之一。 #to_json 方法是转义unicode字符。这可以通过以下方式观察:

  user.to_json 

#=> {\ created_at\ :\ 2012-04-19T18:48:01Z\ \ email\:\ tr\\\\ädg\\\\år @示例.com\,\id\:10,\updated_at\:\2012-04-27T18:37:10Z\}

然而,当被解析时,这被转换回你的期望。然而,可以使用 JSON.generate 生成JSON,可以使用 #as_json 方法,以及施工的任何选择。这不排除unicode。要做到这一点:

  JSON.generate(user.as_json)

#=> {\ created_at\ :\ 2012-04-19T18:48:01Z\ \ email\:\ trädgår@example.com\,\ ID \:10,\updated_at\:\2012-04-27T18:37:10Z\}


I've got a Rails 3.2 app running on Ruby 1.9.3 that returns JSON data stored in a MongoDB database. The data seems to be stored correctly in mongo, e.g. (look at the name attribute):

{ "_id" : ObjectId("4f986cbe4c8086fdc9000002"), "created_at" : ISODate("2012-04-25T21:31:45.474Z"), "updated_at" : ISODate("2012-04-26T22:07:23.901Z"), "creator_id" : ObjectId("4f6b4d3c4c80864381000001"), "updater_id" : null, "name" : "Trädgår'n", "sort" : "tradgarn", "address" : "Nya Allén 11", "coordinates" : [ 11.9764791, 57.7045625 ], "phone" : "46031102080", "url" : "http://www.profilrestauranger.se/tradgarn/", "user_ids" : [ ] }

But when I issue a request that returns this record, I get something like this back (now look at the name attribute):

{"address":"Nya All\u00e9n 11","coordinates":[11.9764791,57.7045625],"created_at":"2012-04-25T23:31:45+02:00","id":"4f986cbe4c8086fdc9000002","name":"Tr\u00e4dg\u00e5r'n","phone":"46031102080","sort":"tradgarn","updated_at":"2012-04-27T00:07:23+02:00","url":"http://www.profilrestauranger.se/tradgarn/"}

The response headers for anyone interested:

HTTP/1.1 200 OK
Server: nginx/1.2.0
Date: Thu, 26 Apr 2012 22:41:13 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 909
Connection: keep-alive
Status: 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Max-Age: 1000
Access-Control-Allow-Headers: *,x-requested-with
X-UA-Compatible: IE=Edge
ETag: "d2a95f06bec10d8087c3188280292d3c"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: fdd042568195df279e59affe45bdcd37
X-Runtime: 0.037134

I cannot seem to figure out why or where the encoding is getting messed up? What gives? Help? :)

解决方案

The issue is indeed one of JSON UTF-8 encoding. The #to_json method is escaping unicode characters. This can be observed by something like:

user.to_json

#  => "{\"created_at\":\"2012-04-19T18:48:01Z\",\"email\":\"tr\\u00e4dg\\u00e5r@example.com\",\"id\":10,\"updated_at\":\"2012-04-27T18:37:10Z\"}"

When parsed, however, this is converted back to how you would expect it. It is possible, however, to generate the JSON using JSON.generate, with which the #as_json method can be used, along with any options for the construction. This doesn't escape the unicode. To do such:

JSON.generate(user.as_json)

#  => "{\"created_at\":\"2012-04-19T18:48:01Z\",\"email\":\"trädgår@example.com\",\"id\":10,\"updated_at\":\"2012-04-27T18:37:10Z\"}"

这篇关于Rails UTF-8响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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