Ruby on rails Rails 0mPG::错误:错误:编码的字节序列无效;UTF8";:0xeda0bc

Ruby on rails Rails 0mPG::错误:错误:编码的字节序列无效;UTF8";:0xeda0bc,ruby-on-rails,ruby,postgresql,utf-8,rails-postgresql,Ruby On Rails,Ruby,Postgresql,Utf 8,Rails Postgresql,我在尝试将tweets写入我的psql数据库时遇到错误 我在互联网上到处搜索(也许还不够好),都没有找到答案。我已经看了这里的答案,但建议将字符串转换为UTF8(尽管响应标题声称它已经是UTF-8) 我是这样做的: # get the data from twitter response = RestClient.get "http://search.twitter.com/search.json?rpp=100&since_id=238726971826253824&q=lov

我在尝试将tweets写入我的psql数据库时遇到错误

我在互联网上到处搜索(也许还不够好),都没有找到答案。我已经看了这里的答案,但建议将字符串转换为UTF8(尽管响应标题声称它已经是UTF-8)

我是这样做的:

# get the data from twitter
response = RestClient.get "http://search.twitter.com/search.json?rpp=100&since_id=238726971826253824&q=love"

# find the data encoding using CharDet
data = CharDet.detect(response.body)
encoding = data['encoding']

# create a new instance of Iconv with UTF-8 and then convert response.body
ic = Iconv.new('UTF-8//IGNORE', encoding)
converted_response = ic.iconv(response.body + '  ')[0..-2]

# take the data and convert it to JSON
response_json = ActiveSupport::JSON.decode(converted_response)

然后,我们解析response_json并从内到外创建tweets数据库。但是,在执行此操作时,我们会在下面看到此错误。

我已经测试了response_json(returns Hash)的类,尽管在该错误的末尾它说hashWithInferenceTaccess

还有谁有类似的问题&知道解决方案吗


谢谢

我找到了一个有效的解决方案!不确定这是否是最好的例子,因为我对Rails/Ruby还不熟悉——但它似乎至少在目前起到了作用

正如您在上面的示例中所看到的,我试图将整个response.body转换为UTF-8。这被证明是不成功的

在查看正在检索的数据时,唯一可能具有非UTF-8实体的部分是tweet状态文本。Twitter不允许在其显示名称中使用非a-z、-、字符。由于我只存储显示名称、状态文本和tweet ID,因此状态文本将被保留。看看推特上的一些状态——一些用户在推特上使用了表情符号等

我的解决方案是将单个状态文本转换为UTF-8,然后在散列中重新分配它

def parse_response!
tweets_json = response_json['results'].reverse rescue []
tweets << tweets_json.collect do |tweet_json|

  # trying to fix encoding issue!
  data = CharDet.detect(tweet_json['text'])
  encoding = data['encoding']
  ic = Iconv.new('UTF-8//IGNORE', encoding)
  converted_response = ic.iconv(tweet_json['text'] + '  ')[0..-2]
  # after converting, put back into value
  tweet_json['text'] = converted_response

  # ... etc
def parse_响应!
tweets_json=response_json['results'].反向救援[]

我在MacOSX上本地运行,在Heroku上运行。问题出在两端。你能展示文本的原始的、未混合的形式吗?一般来说,字符集处理看起来非常可疑。字符集检测充其量是不确定的,用于自动转换的基础很差。第二,
ActiveSupport::JSON.decode()
是否将JSON转换为Ruby数据结构?您的注释和变量名有点让人困惑。至于声称是UTF-8的响应头:这假设实现API的人对文本编码有一半的线索,发送推文的客户端也有。只需要一个人就可以将ISO-8859-1(“拉丁语-1”)字节塞进一个假定的UTF-8字符串。@CraigRinger你说的UTF-8声明完全正确!他们可能会说这是一种特殊的编码方式,而没有先对其进行消毒。
def parse_response!
tweets_json = response_json['results'].reverse rescue []
tweets << tweets_json.collect do |tweet_json|

  # trying to fix encoding issue!
  data = CharDet.detect(tweet_json['text'])
  encoding = data['encoding']
  ic = Iconv.new('UTF-8//IGNORE', encoding)
  converted_response = ic.iconv(tweet_json['text'] + '  ')[0..-2]
  # after converting, put back into value
  tweet_json['text'] = converted_response

  # ... etc