我需要将json-string转换为python对象。按对象我的意思是“新”python3对象,如:如何将json转换为对象?
class MyClass(object):
我发现了几个帮助例如jsonpickle文档。但我发现的所有教程都是先将对象转换为json,然后再向后转换。
我想从Rest-API转换json字符串。
这是我迄今所做的:
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?我怎么知道目标列表应该是List<Goal>
?
'OBJ = jsonpickle.decode(result.content)'=>这会给你的字典。 – falsetru
'obj = result.json()'也会给你一个字典。 – falsetru