【问题标题】:Why can't python parse this json string into a dictionary?为什么python不能把这个json字符串解析成字典?
【发布时间】:2020-02-01 14:15:08
【问题描述】:

这是我试图在 python 3.8 中解析成字典变量的字符串:

str = '{"asdasd": {"username": "asdsad", "filename": "asdsad", "password": "asdsa", "accounts": "11", "headless": True}}'

当我跑步时:

import json
json.loads(str) 

这是引发的错误:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    json.loads(str)
  File "C:\Users\kaspe\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\kaspe\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\kaspe\AppData\Local\Programs\Python\Python38-32\lib\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 108 (char 107)

任何帮助将不胜感激!

【问题讨论】:

  • 未来,第一步是在验证工具中尝试您的 JSON,网上有很多(例如 jsonlint.com)。它将为您节省很多头撞。
  • @Lars Mohammed 如果这解决了您的问题,您能否将答案标记为已接受。

标签: json python-3.x dictionary


【解决方案1】:

您的 str 变量不包含有效的 JSON。 应该是

str = '{"asdasd": {"username": "asdsad", "filename": "asdsad", "password": "asdsa", "accounts": "11", "headless": true}}'

在 JSON 中,布尔值表示为真/假,在 Python 中表示为真/假。

【讨论】:

  • 找出问题所在的一种方法是错误输出告诉您字符串的 char 107。 str[107]T,属于 True,作为此答案状态,JSON 无效
猜你喜欢
  • 2012-06-20
  • 1970-01-01
  • 2015-03-20
  • 1970-01-01
  • 2015-09-17
  • 1970-01-01
  • 2017-02-04
相关资源
最近更新 更多