将 Json 转换为 Python Object(一)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ cat example.py
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)

1
2
3
$ python example.py
test1
2013-11-12