我需要將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