我在python中遇到了JSon的问题

时间:2018-04-25 09:58:30

标签: python json python-3.x

运行脚本时出现此错误? 谁知道主要问题是什么?已导入json并安装了simplejson,它仍然存在。

{'error': {'code': 400, 'message': 'Bad Request'}}
72
Traceback (most recent call last):
  File "test.py", line 212, in <module>
    main()
  File "test.py", line 203, in main
    found = ticket_swap_bot.gather_data_for_event(event)
  File "test.py", line 178, in gather_data_for_event
    if r.json().get('success', False):
  File "C:\Users\x\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\models.py", line 892, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\x\AppData\Local\Programs\Python\Python36\lib\site-packages\simplejson\__init__.py", line 518, in loads
    return _default_decoder.decode(s)
  File "C:\Users\x\AppData\Local\Programs\Python\Python36\lib\site-packages\simplejson\decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "C:\Users\x\AppData\Local\Programs\Python\Python36\lib\site-packages\simplejson\decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

1 个答案:

答案 0 :(得分:0)

当您使用json.loads()时,您应该将字符串传递给它。 根据你的问题,你正在将字典传递给它,这对于使用" json键和值很重要,看看他的例子,这可能会对你有帮助:

 > j = '{"error": {"code": 400, "message": "Bad Request"}}'
 > json.loads(j)
 {'error': {'code': 400, 'message': 'Bad Request'}}

这是一个糟糕的方法:

> a = "{'error': {'code': 400, 'message': 'Bad Request'}}" # a is string but not json format
> json.loads(a)

JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

还有一个错误的方法:

> a = {'error': {'code': 400, 'message': 'Bad Request'} # a is a dict
> json.loads(a)

TypeError: the JSON object must be str, not 'dict'