-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON.py
More file actions
28 lines (20 loc) · 674 Bytes
/
JSON.py
File metadata and controls
28 lines (20 loc) · 674 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
d = dict(name="Bob", age=20, score=88)
data = json.dumps(d)
print("JSON Data is a str:", data)
reborn = json.loads(data)
print(reborn)
class Student(object):
def __init__(self, name, age, score):
self.name = name
self.age = age
self.score = score
def __str__(self):
return "Student object (%s, %s, %s)" % (self.name, self.age, self.score)
s = Student("Bob", 20, 88)
std_data = json.dumps(s, default=lambda obj: obj.__dict__)
print("Dump Student:", std_data)
rebuild = json.loads(std_data, object_hook=lambda d: Student(d["name"], d["age"], d["score"]))
print(rebuild)