用Ruby重写字符串中的字符 [英] Unescaping characters in a string with Ruby
问题描述
s =\ \\\
如何将其转换为实际的ascii字符,以便 s =< p>
?
在OSX上,我成功使用了 Iconv .iconv('ascii','java',s)
但是一旦部署到Heroku,我会收到一个 Iconv :: IllegalSequence
exception。我猜,Heroku部署的系统不支持 java
编码器。
我正在使用 HTTParty 向Posterous API发出请求。如果我使用卷曲来做出相同的请求,那么我不要获得双斜杠。
从HTTParty github页面:
将JSON和XML自动解析为
基于响应的ruby哈希
content-type
Posterous API返回JSON(无双斜杠)和HTTParty的JSON解析正在插入双斜杠。
以下是我使用HTTParty发出请求的一个简单示例。
class Posterous
include HTTParty
base_urihttp://www.posterous.com/api / 2
basic_authusername,password
格式:json
def get_posts
response = Posterous.get(/ users / me / sites / 9876 / posts& api_token = 1234)
#snip,see below ...
end
end
将明显的信息(用户名,密码,site_id,api_token)替换为有效值。
在剪辑点, response.body
包含一个Ruby字符串,我s的JSON格式和 response.parsed_response
包含一个Ruby哈希对象,它通过解析来自Posterous API的JSON响应创建的。
在这两种情况下,诸如 \\\<
的unicode序列已更改为 \\003C
我遇到了这个确切的问题。 HTTParty使用的json解析器(Crack gem)中存在一个错误 - 基本上它对Unicode序列使用区分大小写的正则表达式,因此,由于Posterous将A-F代替为-f,所以Crack不会对其进行转义。我提交了一个拉动请求来解决这个问题。
在此期间,HTTParty很好地让您指定替代解析器,以便您可以执行 :: JSON.parse
绕过破解完全如下所示:
class JsonParser< HTTParty :: Parser
def json
:: JSON.parse(body)
end
end
class Posterous
include HTTParty
parser :: JsonParser
#....
end
Given a string in the following format (the Posterous API returns posts in this format):
s="\\u003Cp\\u003E"
How can I convert it to the actual ascii characters such that s="<p>"
?
On OSX, I successfully used Iconv.iconv('ascii', 'java', s)
but once deployed to Heroku, I receive an Iconv::IllegalSequence
exception. I'm guessing that the system Heroku deploys to does't support the java
encoder.
I am using HTTParty to make a request to the Posterous API. If I use curl to make the same request then I do not get the double slashes.
From HTTParty github page:
Automatic parsing of JSON and XML into ruby hashes based on response content-type
The Posterous API returns JSON (no double slashes) and HTTParty's JSON parsing is inserting the double slash.
Here is a simple example of the way I am using HTTParty to make the request.
class Posterous
include HTTParty
base_uri "http://www.posterous.com/api/2"
basic_auth "username", "password"
format :json
def get_posts
response = Posterous.get("/users/me/sites/9876/posts&api_token=1234")
# snip, see below...
end
end
With the obvious information (username, password, site_id, api_token) replaced with valid values.
At the point of snip, response.body
contains a Ruby string that is in JSON format and response.parsed_response
contains a Ruby hash object which HTTParty created by parsing the JSON response from the Posterous API.
In both cases the unicode sequences such as \u003C
have been changed to \\u003C
.
I ran into this exact problem the other day. There is a bug in the json parser that HTTParty uses (Crack gem) - basically it uses a case-sensitive regexp for the Unicode sequences, so because Posterous puts out A-F instead of a-f, Crack isn't unescaping them. I submitted a pull request to fix this.
In the meantime HTTParty nicely lets you specify alternate parsers so you can do ::JSON.parse
bypassing Crack entirely like this:
class JsonParser < HTTParty::Parser
def json
::JSON.parse(body)
end
end
class Posterous
include HTTParty
parser ::JsonParser
#....
end
这篇关于用Ruby重写字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!