hermanschutte
hermanschutte

Reputation: 663

Using json_decode on a JSON feed containing Cyrillic characters

I'm trying to decode a JSON feed containing some Cyrillic characters. Not all of the characters in the feed is Cyrillic though. I'm using json_decode which works fine for anything else, but return garbage when there are Cyrillic characters.

The results look like this: Деффачки

Any ideas?

Upvotes: 2

Views: 2624

Answers (3)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798726

Your page is being decoded as CP1252 when it's actually UTF-8. Set your headers properly.

>>> print u'Деффачки'.encode('cp1252').decode('utf-8')
Деффачки

Upvotes: 1

Zhasulan Berdibekov
Zhasulan Berdibekov

Reputation: 1087

hermanschutte Use the escape function when sending data through javascript

Upvotes: 0

Ish
Ish

Reputation: 29546

if you can not decode unicode characters with json_decode, use addslashes() while using json_encode. The problem comes from unicode chars starting with \ such as \u30d7

$json_data = addslashes(json_encode($unicode_string_or_array));

Upvotes: 0

Related Questions