Encoding::UndefinedConversionError

后端 未结 5 599
天命终不由人
天命终不由人 2020-12-13 09:09

I keep getting an Encoding::UndefinedConversionError - "\\xC2" from ASCII-8BIT to UTF-8 every time I try to convert a hash into a JSON string. I tried

5条回答
  •  自闭症患者
    2020-12-13 10:03

    If you don't care about losing the strange characters, you can blow them away:

    str.force_encoding("ASCII-8BIT").encode('UTF-8', undef: :replace, replace: '')
    

提交回复
热议问题