I have a web service in rails (3.2.19) that encode a JSON to be read by some iOS or Android apps.
The json might contain any characters, but it seems to fail every time I use a 4 bytes UTF8 character such as 💩
: it produce instead \uf4a9
aka ï’©
.
In rails console the character is correctly displayed but when i retrieve the answer in iOS with AFNetworking or with HTTParty, it fails.
Here is my code sample to retrieve the faulty JSON:
puts HTTParty.post( 'http://0.0.0.0:3000/login',
:body => { :login => 'antoine',
:password => 'thisisnotmypassword',
}.to_json,
:headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json' })
And to encode the JSON:
format.json { render json: json_reponse } #json_response is a Hash.
Sending other UTF-8 characters works well, for exemple: ضصيتØضصتØخـ
Your web service is encoding the JSON incorrectly. It looks like you're not using the standard JSON encoder, since as of Rails 3.2.13, the Unicode characters are passed through (that is, you shouldn't be seeing any \u1234
-type encoding.)
That said, if you want to maintain the old encoding, try switching from object.to_json
to JSON.generate(object, :ascii_only => true).
(More details in the link in the 1st paragraph.)
In the end, I used
JSON::dump(obj))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With