1

I have JSON result

{"sUrlRedirect":"http://dez.loc/registration","sMsgTitle":null,"sMsg":"\u041f\u043e\u0437\u0434\u0440\u0430\u0432\u043b\u044f\u0435\u043c! \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0448\u043b\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e","bStateError":false}

How i can decode it in python. Results must be such like this

{"sUrlRedirect":"http://dez.loc/registration","sMsgTitle":null,"sMsg":"Поздравляем! Регистрация прошла успешно","bStateError":false}

Thanks...

UPD

Can i do this without using json module?

Patrick Burns
  • 1,763
  • 6
  • 21
  • 35
  • 1
    Try reading this: http://stackoverflow.com/questions/990169/how-do-convert-unicode-escape-sequences-to-unicode-characters-in-a-python-string – dave May 02 '13 at 19:29
  • @dave: That won't work very well for this. This question isn't about decoding escape sequences, but an entire JSON document. JSON escape sequences are a small part, and they are incompatible with Python's escape sequences (due to UTF-16 weirdness). – Dietrich Epp May 02 '13 at 19:34
  • Why do you want to do it without the `json` module? That's like asking how to calculate a square root without a calculator or slide rule. Sure, you can do it by hand, but it will take forever. – Dietrich Epp May 02 '13 at 19:39
  • @DietrichEpp You're right... I was implying the use of `import json`. – dave May 02 '13 at 19:42
  • Ok, ok :) Thanks all very much. I think, i have more roads to decode this result... – Patrick Burns May 02 '13 at 19:45

2 Answers2

4

Just use inbuilt python json module to load the json as python object, you will however see that your unicode strings are represented as '\u041f' , when you use them in your application, it should appear fine as Russian text.

>>> json_str= '{"sUrlRedirect":"http://dez.loc/registration","sMsgTitle":null,"sMsg":"\u041f\u043e\u0437\u0434\u0440\u0430\u0432\u043b\u044f\u0435\u043c! \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0448\u043b\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e","bStateError":false}'
>>> import json
>>> the_dict = json.loads(json_str)
>>> the_dict
{u'sMsgTitle': None, u'bStateError': False, u'sMsg': u'\u041f\u043e\u0437\u0434\u0440\u0430\u0432\u043b\u044f\u0435\u043c! \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0448\u043b\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e', u'sUrlRedirect': u'http://dez.loc/registration'}

>>> print the_dict['sMsg']
Поздравляем! Регистрация прошла успешно
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
2

Use the json module:

In [1]: import json

In [2]: s = '''{"sUrlRedirect":"http://dez.loc/registration","sMsgTitle":null,"sMsg":"\u041f\u043e\u0437\u0434\u0440\u0430\u0432\u043b\u044f\u0435\u043c! \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0448\u043b\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e","bStateError":false}'''

In [3]: json.loads(s)
Out[3]: 
{u'bStateError': False,
 u'sMsg': u'\u041f\u043e\u0437\u0434\u0440\u0430\u0432\u043b\u044f\u0435\u043c! \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0448\u043b\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e',
 u'sMsgTitle': None,
 u'sUrlRedirect': u'http://dez.loc/registration'}

In [4]: for k, v in json.loads(s).iteritems():
            print k, v
   ...:     
sMsgTitle None
bStateError False
sMsg Поздравляем! Регистрация прошла успешно
sUrlRedirect http://dez.loc/registration

In [5]: print repr(json.loads(s)).decode("unicode-escape")
{u'sMsgTitle': None, u'bStateError': False, u'sMsg': u'Поздравляем! Регистрация прошла успешно', u'sUrlRedirect': u'http://dez.loc/registration'}
root
  • 76,608
  • 25
  • 108
  • 120