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?

5
  • 1
    Try reading this: stackoverflow.com/questions/990169/… Commented May 2, 2013 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). Commented May 2, 2013 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. Commented May 2, 2013 at 19:39
  • @DietrichEpp You're right... I was implying the use of import json. Commented May 2, 2013 at 19:42
  • Ok, ok :) Thanks all very much. I think, i have more roads to decode this result... Commented May 2, 2013 at 19:45

2 Answers 2

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']
Поздравляем! Регистрация прошла успешно
Sign up to request clarification or add additional context in comments.

3 Comments

Can i do this without using json module?
you could use simplejson instead ;)
Or, if you have too much time on your hands, you could implement an entire syntax parser based on json.org
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'}

2 Comments

Can i do this without using json module?
If you are willing to write your own parser...I can't think of a reason why you should though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.