收到错误 JSONDecodeError: 从 API 导入时预期 ',' 分隔符。

huangapple go评论75阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2020年10月5日 14:22:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64203359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定