【问题标题】:json.decoder.JSONDecodeError: Expecting value: , json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes:json.decoder.JSONDecodeError: 期望值: , json.decoder.JSONDecodeError: 期望用双引号括起来的属性名称:
【发布时间】:2019-01-04 06:31:49
【问题描述】:

您好,我正在 Python 文件中使用 JSON:

import json
userData = '''[
{
    "userID" : "20",
    "devices" : {
        "360020000147343433313337" : "V33_03",
        "3f0026001747343438323536" : "Door_03",
        "170035001247343438323536" : "IR_06",
        "28004c000651353530373132" : "BED_17"
    }
},

]'''

info = json.loads(userData)

加载时出现此错误: json.decoder.JSONDecodeError:期望值:

或者有时当我添加一些东西时: json.decoder.JSONDecodeError:期望用双引号括起来的属性名称:

【问题讨论】:

    标签: python json python-jsonschema


    【解决方案1】:

    尝试使用ast 模块

    例如:

    import ast
    userData = '''[
    {
        "userID" : "20",
        "devices" : {
            "360020000147343433313337" : "V33_03",
            "3f0026001747343438323536" : "Door_03",
            "170035001247343438323536" : "IR_06",
            "28004c000651353530373132" : "BED_17"
        }
    },
    ]'''
    
    info = ast.literal_eval(userData)
    print(info)
    

    【讨论】:

    • 这是一个 JSON 对象吗?
    • JSON 对象是什么意思?使用ast 将您的字符串数据结构转换为有效的python 数据类型..就像dict 一样json.loads
    • 我可以通过 info['userID'] 获取对象?,是的,刚刚测试过,我可以这样做。但是,仍然想知道这不起作用...
    【解决方案2】:

    看起来格式不正确。

    userData = '''[
    {
        "userID" : "20",
        "devices" : {
            "360020000147343433313337" : "V33_03",
            "3f0026001747343438323536" : "Door_03",
            "170035001247343438323536" : "IR_06",
            "28004c000651353530373132" : "BED_17"
        }
    },  <--- remove this ","
    
    ]'''
    

    查看我的测试:

    >>> import json
    >>> json.loads('[{"a":"b"}]')
    [{u'a': u'b'}]
    >>> json.loads('[{"a":"b"},]')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python27\lib\json\__init__.py", line 338, in loads
        return _default_decoder.decode(s)
      File "C:\Python27\lib\json\decoder.py", line 366, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode
        raise ValueError("No JSON object could be decoded")
    ValueError: No JSON object could be decoded
    >>>
    

    【讨论】:

      【解决方案3】:

      为了将来参考,下面如何获取 JSON 内容或获取一些垃圾邮件:

      import requests
      
      
      url = 'http://httpbin.org/status/200'
      r = requests.get(url)
      
      if 'json' in r.headers.get('Content-Type'):
          js = r.json()
      else:
          print('Response content is not in JSON format.')
          js = 'spam'
      

      【讨论】:

      • 这对原始问题有帮助吗? (识别 JSON 字符串中的问题)
      【解决方案4】:

      按照你的例子,没有进一步的理解: info = json.loads(json.dumps(userData)) 会起作用。

      在 SO 上有很多关于 python 多行字符串和 JSON 的帖子。理想情况下,您不会从字符串变量中加载字符串,这是您将看到的一般注释。

      通过一些额外的解释,例如数据的来源和格式,我可以提供额外的帮助。

      【讨论】:

        猜你喜欢
        • 2018-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-22
        • 1970-01-01
        相关资源
        最近更新 更多