how to convert json to python class?
json, python
Solution
Use `object_hook` special parameter in load functions of json module:
import json
class JSONObject:
def __init__( self, dict ):
vars(self).update( dict )
#this is valid json string
data='{"channel":{"lastBuild":"2013-11-12", "component":["test1", "test2"]}}'
jsonobject = json.loads( data, object_hook= JSONObject)
print( jsonobject.channel.component[0] )
print( jsonobject.channel.lastBuild )
This method have some issue, like some names in python are reserved. You can filter them out inside `__init__` method.
Problem
I want to Json to Python class. example ``` {'channel':{'lastBuild':'2013-11-12', 'component':['test1', 'test2']}} self.channel.component[0] => 'test1' self.channel.lastBuild => '2013-11-12' ``` do you know python library of json converting?