简体   繁体   English

如何修复 JSONDecodeError:期望值:第 2 行第 1 列(字符 1)

[英]How to fix JSONDecodeError: Expecting value: line 2 column 1 (char 1)

I'm trying to follow along with this interactive notebook developed by Google to train an ML model from satellite images: https://colab.research.google.com/github/GoogleCloudPlatform/python-docs-samples/blob/main/people-and-planet-ai/geospatial-classification/README.ipynb#scrollTo=77c0f36a我正在尝试使用由 Google 开发的交互式笔记本来训练来自卫星图像的 ML model: https://colab.research.google.com/github/GoogleCloudPlatform/python-docs-samples/blob/main/people -and-planet-ai/geospatial-classification/README.ipynb#scrollTo=77c0f36a

All of this code was written by someone else - I'm just trying to follow along and keep ending up with quite a few errors.所有这些代码都是由其他人编写的——我只是想继续跟进并不断出现很多错误。 I've managed to work around some (I essentially have no coding knowledge at all), but some are beyond my skill level.我设法解决了一些问题(我基本上根本没有编码知识),但有些超出了我的技能水平。 I'm running all of this in a Google Colab notebook.我在 Google Colab 笔记本中运行所有这些。

I keep running into problems with this specific cell of code:我一直遇到这个特定代码单元的问题:

import requests

requests.post(
    url=f'{service_url}/ping',
    headers={'Authorization': f'Bearer {identity_token}'},
    json={'x': 42, 'message': 'Hello world!'},
).json()

The error output is the following:错误 output 如下:

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-43-3a865e581f23> in <module>()
      4     url=f'{service_url}/ping',
      5     headers={'Authorization': f'Bearer {identity_token}'},
----> 6     json={'x': 42, 'message': 'Hello world!'},
      7 ).json()

3 frames
/usr/local/lib/python3.7/dist-packages/requests/models.py in json(self, **kwargs)
    896                     # used.
    897                     pass
--> 898         return complexjson.loads(self.text, **kwargs)
    899 
    900     @property

/usr/lib/python3.7/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

/usr/lib/python3.7/json/decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

/usr/lib/python3.7/json/decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 2 column 1 (char 1)

Could someone explain to me (like you would to a 5-year-old) what's going wrong with the JSON parameter?有人可以向我解释(就像你对 5 岁的孩子一样) JSON 参数出了什么问题?

Your service_url might be sending some other response (not Json).您的service_url可能正在发送一些其他响应(不是 Json)。 To view this use resp.text and then try to convert it to json using try and except method要查看此内容,请使用resp.text ,然后尝试使用try and except方法将其转换为 json

resp=requests.post(<your_headers_and_url>)

#Print output in text format
print(resp.text)

Sample Code for Testing测试示例代码

import requests

service_url="https://httpbin.org/"
identity_token="AABBCCDD"
resp=requests.post(
    url=f'{service_url}/post',
    headers={'Authorization': f'Bearer {identity_token}'},
    json={'x': 42, 'message': 'Hello world!'},
)

#Print the response in text format
#print(resp.text)

#Then try to convert it to json 
#using try and except method to catch the error
try:
    print(resp.json())
except:
    print("Not Json")

Try online compiler to test the code https://www.programiz.com/python-programming/online-compiler/尝试在线编译器测试代码https://www.programiz.com/python-programming/online-compiler/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 'json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)' 错误? - How to fix 'json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)' error? 如何修复 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)? - How can fix json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)? JSONDecodeError:期望值:python 中的第 1 行第 1 列(字符 0) - JSONDecodeError: Expecting value: line 1 column 1 (char 0) in python 熊猫:JSONDecodeError:期望值:第 1 行第 1 列(字符 0) - Pandas: JSONDecodeError: Expecting value: line 1 column 1 (char 0) JSONDecodeError:期望值:第 1 行第 1 列(字符 0) - JSONDecodeError: Expecting value: line 1 column 1 (char 0) 得到 JSONDecodeError:期望值:第 1 行第 1 列(字符 0) - Got JSONDecodeError: Expecting value: line 1 column 1 (char 0) JSONDecodeError:期望值:第 6 行第 1 列(字符 5) - JSONDecodeError: Expecting value: line 6 column 1 (char 5) JSONDecodeError:预期值:第1行第1列(字符0)错误 - JSONDecodeError: Expecting value: line 1 column 1 (char 0) error ExtensionFailed JSONDecodeError:预期值:第 1 行第 1 列(字符 0) - ExtensionFailed JSONDecodeError: Expecting value: line 1 column 1 (char 0) JSONDecodeError 期望值:第 2 行第 5 列(字符 5) - JSONDecodeError Expecting value: line 2 column 5 (char 5)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM