将包含 \x 个字符的编码字符串转换为 JSON
Convert encoded string containg \x characters to JSON
我正在尝试转换为 JSON,一个包含反斜杠 x 编码字符的字符串
data = "{\"schema\":\"iglu\", \"data\": \"hellox\E2\x80\x99s\"}"
data.to_json
但是我收到以下错误:
JSON::GeneratorError: 源序列是 illegal/malformed utf-8
来自 /Users/user1/.rvm/gems/ruby-2.5.5@tagtrue/gems/activesupport-4.2.11.1/lib/active_support/core_ext/object/json.rb:34:in `to_json'
由 Encoding::UndefinedConversionError: "\xE2" from ASCII-8BIT to UTF-8
引起
如何让to_json传递数据?
这个解决方案对我有用:
data.force_encoding('utf-8')
我正在尝试转换为 JSON,一个包含反斜杠 x 编码字符的字符串
data = "{\"schema\":\"iglu\", \"data\": \"hellox\E2\x80\x99s\"}"
data.to_json
但是我收到以下错误: JSON::GeneratorError: 源序列是 illegal/malformed utf-8 来自 /Users/user1/.rvm/gems/ruby-2.5.5@tagtrue/gems/activesupport-4.2.11.1/lib/active_support/core_ext/object/json.rb:34:in `to_json' 由 Encoding::UndefinedConversionError: "\xE2" from ASCII-8BIT to UTF-8
引起如何让to_json传递数据?
这个解决方案对我有用:
data.force_encoding('utf-8')