首页 > 代码库 > json

json

json主要是用来不同语言间的格式转换,

import json
class Student(object):
def __init__(self, name, age, score):
self.name = name
self.age = age
self.score = score
s = Student(Bob, 20, 88)
print(json.dumps(s))

def student2dict(std):
return {
name: std.name,
age: std.age,
score: std.score
}

>>> print(json.dumps(s, default=student2dict))
{"age": 20, "name": "Bob", "score": 88}

print(json.dumps(s, default=lambda obj: obj.__dict__))

一般使用dumps()和loads()来进行序列化和反序列化

json