feat(web基础): 新增零基础Web开发课程
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
"""第5-4课标准示例:完成Python对象与JSON文本的双向转换。"""
|
||||
|
||||
import json
|
||||
|
||||
|
||||
def serialize_book() -> str:
|
||||
"""把Python图书对象序列化为JSON文本。"""
|
||||
|
||||
book = {
|
||||
"id": 1,
|
||||
"title": "Python Web基础",
|
||||
"price": 59.9,
|
||||
"available": True,
|
||||
"tags": ["Python", "Web"],
|
||||
"description": None,
|
||||
}
|
||||
return json.dumps(book, ensure_ascii=False, indent=2)
|
||||
|
||||
|
||||
def deserialize_request(request_body: str) -> dict:
|
||||
"""把客户端发送的JSON文本反序列化为Python字典。"""
|
||||
|
||||
return json.loads(request_body)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""依次展示序列化和反序列化结果。"""
|
||||
|
||||
print("JSON响应:")
|
||||
print(serialize_book())
|
||||
request_data = deserialize_request('{"title": "HTTP实践", "price": 49.0}')
|
||||
print(f"请求中的书名:{request_data['title']}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user