【问题标题】:TypeError: unbound method decode() must be called with JSONDecoder instance as first argument (got PrintJson instance instead)TypeError:必须使用 JSONDecoder 实例作为第一个参数调用未绑定的方法 decode()(改为获取 PrintJson 实例)
【发布时间】:2015-12-01 08:05:11
【问题描述】:

TypeError: 必须使用 JSONDecoder 实例作为第一个参数调用未绑定方法 decode()(改为获取 PrintJson 实例)

当我尝试将 python 对象解析为 json 时出现此错误。

使用以下代码:

import json
from json import JSONDecoder

class TestJson():
    name = None
    pass

printJson = PrintJson()
print printJson
print JSONDecoder.decode(printJson) // at this line, getting this error

【问题讨论】:

    标签: python json python-2.7


    【解决方案1】:

    这是因为您在 JSONDecoder 类上调用该方法,而不是在它的实例上。这会起作用:

    from json import JSONDecoder()
    JSONDecoder().decode('{}')
    

    【讨论】:

    • 感谢您的回复。但这对我来说不起作用。在遵循给定的指令后,得到下面提到的错误:- JSONDecoder().decode(printJson) 文件“C:\Python27\lib”中的文件“C:\Users\abc\test\json_test.py”,第 69 行\json\decoder.py", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) TypeError: expected string or buffer
    • 出现错误:- Traceback(最近一次调用最后一次):文件“C:/Users/jagdeep/test/json_test2.py”,第 15 行,在 print(jd.decode(str (printJson))) 文件“C:\Python27\lib\json\decoder.py”,第 366 行,在 decode obj 中,end = self.raw_decode(s, idx=_w(s, 0).end()) 文件"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
    • 如果你有另一个问题,也许你应该在一个单独的问题中问它
    • 问题和问题都是一样的。我按照之前的建议进行了尝试。那是行不通的。所以我发送错误跟踪。
    猜你喜欢
    • 2017-12-09
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 2018-05-10
    • 2017-04-03
    相关资源
    最近更新 更多