Can't parse simple json with python

前端 未结 2 1530
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-10 09:21

I have a very simple json I can\'t parse with simplejson module. Reproduction:

import simplejson as json
json.loads(r\'{\"translatedatt1\":\"Vari\\351es\"}\'         


        
2条回答
  •  有刺的猬
    2020-12-10 09:48

    You probably did not intend to use a raw string, but a unicode string?

    >>> import simplejson as json
    >>> json.loads(u'{"translatedatt1":"Vari\351es"}')
    {u'translatedatt1': u'Vari\xe9es'}
    

    If you want to quote the data inside the JSON string you need to use \uNNNN:

    >>> json.loads(r'{"translatedatt1":"Vari\u351es"}')
    {'translatedatt1': u'Vari\u351es'}
    

    Please note that the resulting dict is slightly different in this case. When parsing a unicode string simplejson uses unicode strings for the keys. Otherwise it uses byte string keys.

    If your JSON data does in fact use \351e than it is simply broken and no valid JSON.

提交回复
热议问题