gpt4 book ai didi

ruby-on-rails - JSON.load 抛出 Encoding::CompatibilityError

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:52 24 4
gpt4 key购买 nike

我只是想加载 UTF-8 json 文件并返回它。它在 mac 上运行良好,但是当我将它部署到 CentOS 时它失败了。

我该如何解决?

正在加载 UTF-8 文件。

[ec2-user@ip-XXX-XXX-XXX-XXX vfs]$ file data/E03124/data.json 
data/E03124/data.json: UTF-8 Unicode text, with very long lines, with no line terminators

错误信息

Caught Encoding::CompatibilityError at '"{\"資産の部\":{': incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)

回溯

json (1.8.1) lib/json/pure/parser.rb:242:in `rescue in parse_string'
json (1.8.1) lib/json/pure/parser.rb:213:in `parse_string'
json (1.8.1) lib/json/pure/parser.rb:257:in `parse_value'
json (1.8.1) lib/json/pure/parser.rb:121:in `parse'
json (1.8.1) lib/json/common.rb:155:in `parse'
json (1.8.1) lib/json/common.rb:334:in `load'
app/controllers/statements_controller.rb:13:in `block in getData'
app/controllers/statements_controller.rb:12:in `open'
app/controllers/statements_controller.rb:12:in `getData'

rails 代码

def getData
json_data = open("data/#{params[:code]}/data.json") do |io|
JSON.load(io)
end
render :json => json_data
end

Ruby 版本为 2.0.0。

Rails 版本是 4.1.4。

最佳答案

问题是 json/pure 解析器。用于匹配字符串的正则表达式使用 n 选项,这意味着模式采用 ASCII-8BIT 编码。来自ruby regex docs :

A regexp can be matched against a string when they either share an encoding, or the regexp’s encoding is US-ASCII and the string’s encoding is ASCII-compatible.

If a match between incompatible encodings is attempted an Encoding::CompatibilityError exception is raised.

github repository 上打开一个问题.同时尝试不同的解析器。我热烈建议 multi_json yajloj

如果您不能在您的盒子上构建 ruby​​ C 扩展,那就是另一个问题了。当扩展未正确构建时,JSON gem 使用纯变体 (afaik)。

关于ruby-on-rails - JSON.load 抛出 Encoding::CompatibilityError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27673655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com