4

I've a rails controller that respond_to JSON with a wrong encoding. The site is correctly setup with UTF-8 encoding and the database as well. The text in my db is well formatted, but in the JSON response, all special characters are set to \ufffd. How can I solve the problem?

3
  • How are you getting data out of the database? Does it look OK before you JSON-encode it? Have you set encoding: utf8 in your database.yml? Commented Oct 30, 2010 at 20:57
  • yes. the database is utf8, the encoding:utf8 is correct, the string are correctly stored in the db (i double checked them with phpmyadmin). the problem is the way rails encode them with to_json method. Commented Oct 31, 2010 at 13:39
  • What version of ruby and rails are you using? Commented Oct 31, 2010 at 15:03

1 Answer 1

5
+50

Everyone running on Ruby 1.9.x and using MySQL, should use the mysql2 gem. It forces the use of UTF-8 and converts to/from UTF-8 automatically when needed.

Sign up to request clarification or add additional context in comments.

1 Comment

This did not work for me. I'm running Sqlite and characters are written/read from database correctly, however upon using the to_json method all special characters (such as 'åäöячщ') turns into \ufffd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.