python - 尽管 JSON 正确,但奇怪的 json.decoder.JSONDecodeError
问题描述
我正在使用 json.loads 将字符串中的列表转换为列表。(列表在数据传输过程中被转换为字符串)。我收到异常:json.decoder.JSONDecodeError:预期值:第 1 行第 43 列(字符 42)。
本质上,代码如下:
...
string_list = [["select", "What is your age?", [{"18+": True}, {"Below 18": False}], {"boolean": True}, {"application": ["basic", "all"]}, "age"], "18+", True]
replaced_str = str(string_list).replace("'",'"')
data = json.loads(replaced_str)
return data
...
奇怪的是,这个列表:
[["text", "What is your name?", {"question_application": ["basic", "all"]}], "utetwo"]
完美运行。
这是完整的错误回溯:
Traceback (most recent call last):
File "/usr/lib/python3.8/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
StopIteration: 42
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/runner/Giftsling-Prototype-primary-1/main.py", line 129, in searchinput
print(sh.generate_user_attributes(session['user_id']))
File "/home/runner/Giftsling-Prototype-primary-1/main.py", line 260, in generate_user_attributes
d = json.loads(dea)
File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 43 (char 42)
我无法在其他地方找到有关此问题的任何问题。我已按照解决其他人问题的所有步骤进行操作,但无济于事。
解决方案
推荐阅读
- c - 在c中创建一个“tee”流?
- php - 如何将子数组迭代为父数组的“行”,同时(按名称)在每个“行”中回显(键/值对)值
- python - 当我尝试访问中国银行网页时,发送密钥功能不起作用
- c++ - 如何将opencv mat转换为字符串c ++
- aem - 我们可以在 AEM 中使用语言副本翻译页面 URL 吗?
- javascript - 如何使用 React JS 有条件地在对象数组中获取键的长度
- android - React-native 应用程序未显示屏幕
- xml - 使用 Java 将此 xml 文件解析为指定 JSON 的最简单方法是什么?
- flutter - 无法在 Android Studio 中获取或升级 Flutter 的依赖项
- flutter - 颤动 youtube 错误域:ios 上的 XCDYouTubeVideoErrorDomain