首页 > 解决方案 > 尽管 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)

我无法在其他地方找到有关此问题的任何问题。我已按照解决其他人问题的所有步骤进行操作,但无济于事。

标签: pythonjsonflask

解决方案


推荐阅读