How to decode a JSON string to a string, not to unicode

I am trying to decrypt a json dictionary with strings as keys. The result is a dictionary with unicode keys. What is the best way to decode a dictionary with string keys? Better: how can I prevent strings from being decoded to Unicode strings? Of course, after that I can contact ...

What's happening:

>>> import simplejson
>>> simplejson.loads('{"bar":["baz", null, 1.0, 2]}')
{u'bar': [u'baz', None, 1.0, 2]}
>>> simplejson.loads('"bar"')
u'bar'

Desired behavior:

>>> import simplejson
>>> simplejson.loads('{"bar":["baz", null, 1.0, 2]}', ...?)
{'bar': ['baz', None, 1.0, 2]}
>>> simplejson.loads('"bar"', ..?)
'bar'
+3
source share
1 answer

You can not. Encode strings after loading. Or even better, fix the rest of the code so that it doesn't crash when in use unicode.

+2
source

Source: https://habr.com/ru/post/1754950/


All Articles