简体   繁体   中英

Exception when parsing JSON with Python

I am struggling with this, but I don't know which is the problem. I also have tried using .encode('unicode_escape') and it it as a raw string but still not working.

json.loads('{"src":"https://media1.giphy.com/media/deDsaGovR3BMiNh45V/giphy.gif?cid=da5535431178774e95701d0ab60d607f894576a24782a98d&rid=giphy.gif","extraInfo":"{"digsrc":"giphy","digsrc_url":"https://giphy.com/gifs/disenchantment-animation-netflix-deDsaGovR3BMiNh45V"}"}')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\neiu\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\neiu\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\neiu\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 355, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 152 (char 151)```

JSON is not properly formatted, you have "extraInfo":"{" . which is not valid JSON. Also at the end you have }"} which is another format error. The correct string is:

json.loads('{"src":"https://media1.giphy.com/media/deDsaGovR3BMiNh45V/giphy.gif?cid=da5535431178774e95701d0ab60d607f894576a24782a98d&rid=giphy.gif","extraInfo":{"digsrc":"giphy","digsrc_url":"https://giphy.com/gifs/disenchantment-animation-netflix-deDsaGovR3BMiNh45V"}}')

Test it at: https://jsonlint.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM