python - 类型错误 : unbound method decode() must be called with JSONDecoder instance as first argument (got PrintJson instance instead)

标签 python json python-2.7

TypeError: unbound method decode() must be called with JSONDecoder instance as first argument (got PrintJson instance instead)

当我尝试将 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

最佳答案

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

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

关于python - 类型错误 : unbound method decode() must be called with JSONDecoder instance as first argument (got PrintJson instance instead),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32410334/

相关文章:

javascript - 返回特定键的未定义 bool 值的 JSON 字符串的 JSON.parse

python - 将多个值的元组列表转换为 Python 中的字典

java - 如何使用 Gson 反序列化 XMLGregorianCalender?

python - 计算数据框不同列中值的频率

python - python中的行梯队

python - 如何向量化二维数组和另一个向量之间的操作?

c# - 将 TimeSpan 值序列化和反序列化为 Object 类型的属性

python - Relief Ratio - 帮助找到 'polygon zm' 内离流出点最远的点

python - 如何使用随机列表中的特定数字打印python执行时间以输出时间和生成的随机数?

python - 在 Python IDLE session 中显示用户定义的函数列表