英文:
Getting error JSONDecodeError: Expecting ',' delimiter when importing from API
问题
我正试图解析来自API的响应数据,但一直出现这个错误。
这是我的代码:
import requests
import json
url = "https://ratings.food.gov.uk/OpenDataFiles/FHRS776en-GB.json"
r = requests.get(url)
json_data = r.json()
这是错误信息:
File "/usr/local/lib/python3.8/site-packages/requests/models.py", line 889,在 json
return complexjson.loads(
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/__init__.py", line 357,在加载时
return _default_decoder.decode(s)
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 337,在解码时
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 353,在解码raw_decode时
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: 期望 ',' 分隔符:第1行第233384列 (字符 233383)
我确认了这是有效的 JSON,而且这是一个公共API,所以我无法控制格式。我该如何解决这个错误?
英文:
I am trying to parse response data from an API but I keep getting this error.
This is my code:
import requests
import json
url = "https://ratings.food.gov.uk/OpenDataFiles/FHRS776en-GB.json"
r = requests.get(url)
json_data = r.json()
This is the error
File "/usr/local/lib/python3.8/site-packages/requests/models.py", line 889, in json
return complexjson.loads(
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 233384 (char 233383)
I've confirmed this is a valid JSON and this is a public API so I don't have control over the formatting. How can I get past this error?
答案1
得分: 2
问题不在于你的代码,而在于 JSON。
你可以在此处验证 JSON。第 7669 行和第 7670 行各缺少一个逗号。
英文:
The problem is not your code but the json.
You can validate json here. Line 7669 and 7670 are missing a ,
each.
答案2
得分: 0
可以使用
import requests
import json
url = "https://ratings.food.gov.uk/OpenDataFiles/FHRS776en-GB.json"
r = requests.get(url)
json_data = json.loads(r.text)
英文:
You can use
import requests
import json
url = "https://ratings.food.gov.uk/OpenDataFiles/FHRS776en-GB.json"
r = requests.get(url)
json_data = json.loads(r.text)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论