I need to convert a json-string to python object. By object I mean "new" python3 object like:
class MyClass(object):
I found several help for example on jsonpickle documentation. But all I found are tutorials which convert object to json first and after this convert backwards.
I want to convert a json-string from a Rest-API.
Here is what I have done so far:
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)
This results in:
TypeError: the JSON object must be str, bytes or bytearray, not 'method'
It's quite clear to me that jsonpickle can't convert this to my classes (Goal, Match), because I don't tell jsonpickle in which class the output should be converted. The problem is I don't know how to tell jsonpickle to convert the JSON in object from type Match? And how can I tell that the list of goals should be of type List<Goal>
?
obj = jsonpickle.decode(result.content)
=> This will give you a dictionary. – Metabolismobj = result.json()
will also give you a dictionary. – Metabolism