我在 python 中有一个 flask 函数
from flask import jsonify
def method1():
result = {"jsonrpc": "2.0", ... }
return jsonify(result)
接下来,我从另一个方法调用方法 1:
def method2():
otherResult = method1()
...
在方法 2 中,我想获取结果的内容(来自方法 1)。我应该怎么做?问题是如果我尝试这样做:
response = otherResult['jsonrpc']
然后执行时出现如下错误:
TypeError: 'Response' object has no attribute '__getitem__'
调用时
method1,会返回一个 Response 对象给您,其中包含以 的形式传输的数据bytes。要将此数据转换为字典,您需要先对其进行解码,然后使用 json 库将其转换。例如像这样: