我需要将 json-string 转换为 python 对象。通过对象,我的意思是“新”python3 对象,例如:
class MyClass(object):
例如,我在 jsonpickle 文档中找到了一些帮助。但我发现的只是先将对象转换为 json 再向后转换的教程。
我想从Rest-API转换一个 json-string 。
这是我到目前为止所做的:
import requests
import jsonpickle
class Goal(object):
def __init__(self):
self.GoaldID = -1
self.IsPenalty = False
class Match(object):
def __init__(self):
self.Goals = []
headers = {
"Content-Type": "application/json; charset=utf-8"
}
url = "https://www.openligadb.de/api/getmatchdata/39738"
result = requests.get(url=url, headers=headers)
obj = jsonpickle.decode(result.json)
print (obj)
这导致:
TypeError: the JSON object must be str, bytes or bytearray, not 'method'
我很清楚 jsonpickle 无法将其转换为我的类(目标、匹配),因为我没有告诉 jsonpickle 应该在哪个类中转换输出。问题是我不知道如何告诉 jsonpickle 将对象中的 JSON 从匹配类型转换?我怎么知道目标列表应该是 type List<Goal>
?