Найти - Пользователи
Полная версия: Кодировки и JSON
Начало » Python для новичков » Кодировки и JSON
1
i.slepov
Ситуация для Python 3

>>> a = "\u041c\u0430\u0441\u043b\u043e"
>>> a
'Масло'
>>> json.dumps({'a': a})
'{"a": "\\u041c\\u0430\\u0441\\u043b\\u043e"}'
>>> 

Вопрос: Как получить русский текст в JSON?
PooH
А в чем проблема? Это и есть русский текст.
Из RFC 7159:

JSON text SHALL be encoded in UTF-8, UTF-16, or UTF-32. The default
encoding is UTF-8…

If the character is in the Basic
Multilingual Plane (U+0000 through U+FFFF), then it may be
represented as a six-character sequence: a reverse solidus, followed
by the lowercase letter u, followed by four hexadecimal digits that
encode the character's code point. The hexadecimal letters A though
F can be upper or lower case. So, for example, a string containing
only a single reverse solidus character may be represented as
“\u005C”.
J.R.
$ python3
Python 3.3.1 (default, Apr 15 2013, 23:57:40) 
[GCC 4.7.2 20121109 (ALT Linux 4.7.2-alt7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> a = "Масло"
>>> json.dumps({'a': a}, ensure_ascii=False)
'{"a": "Масло"}'
>>> 
i.slepov
ОК. Спасибо.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB