【发布时间】:2019-08-10 12:59:59
【问题描述】:
我收到一个错误:JSONDecodeError: Expecting value: line 1 column 1 (char 0)。但是不明白为什么。
这是我的代码:
import json
import urllib.request
url = "apiurl"
data = json.loads(url)
# Open the URL as Browser, not as python urllib
page = urllib.request.Request(url,headers={'User-Agent': 'Mozilla/5.0'})
infile = urllib.request.urlopen(page).read()
data = infile.decode('ISO-8859-1') # Read the content as string decoded with ISO-8859-1
command_obj = {x['command']: x for x in data}
with open('new_command.json', 'w') as f:
json.dump(command_obj, f, indent=2)
有了这个功能,我只是想从一个 api 中获取数据并修改它的格式。感谢您的帮助
【问题讨论】:
-
您的
url是 JSON 对象的文本表示吗? -
这是一个 json 对象是的!这是网址:api.sheety.co/be29a67e-80b2-4f73-ace9-9a0a2f40f4a6
-
URL 本身 (
"https://api.sheety.co/be29a67e-80b2-4f73-ace9-9a0a2f40f4a6") 是 JSON 对象吗?json.loads(url)将尝试将这个字符串解析为 JSON 对象。