我收到此错误simplejson.errors.JSONDecodeError:预期值:第1行第1列字符0

问题描述

我希望你一切都好。 我正在尝试从此网站“ solcast.com.au”获取太阳辐射值。.我去了他们的api文档,并在这里关注了它“ https://docs.solcast.com.au/#forecasts-by-location',并应用了代码

byte[] theBuffer = null;
mockQueuedSyncPackets.Setup(c => c.Add(It.IsAny<byte[]>()))
        .Callback<byte[]>((obj) => theBuffer = obj);
var theSender = new Sender(mockQueuedSyncPackets.Object);
theSender.SendSync(123,syncData);
Assert.AreEqual(4 + syncData.Length,theBuffer.Length);         

因此,当我运行代码时,出现此错误

import requests
url = 'https://api.solcast.com.au/world_radiation/forecasts?latitude=   -33.865143&longitude=151.209900&api_key=MYAPI'
res = requests.get(url)
data = res.json()
forecast = data["forecasts"]["ghi"]

print('forecastss: {} dgree'.format(forecast))

非常感谢您的帮助。

解决方法

正如John所提到的,您需要在请求中指定您愿意接收的格式。 您可以通过在请求中添加标头来实现:

import requests
url = 'https://api.solcast.com.au/world_radiation/forecasts?latitude=   -33.865143&longitude=151.209900&api_key=API_KEY'
res = requests.get(url,headers={'Content-Type': 'application/json'})
data = res.json()
forecast = data["forecasts"][0]["ghi"]

print('forecastss: {} dgree'.format(forecast))

在他们的文档中,他们提供了两个附加选择:

  1. “接受” HTTP请求标头,例如JSON的“ application / json”
  2. “格式”查询字符串,例如JSON的“ format = json”
  3. 端点后缀文件扩展名,例如JSON的“ forecasts.json”

第二个选项至少在此特定请求中无效。第三个选项有效,但是有点奇怪。

第一个选项在API中更常用,但也准备使用其他选项。

他们在文档中说,

PS“ headers = {'Accepts':'application / json'} 应该给出期望的结果,所以我认为在其他端点中也有可能。

祝你好运