Python的JSON翻译官:让你的数据在异次元穿梭自如
朋友们,想象你有一个会魔法的公文包:能把Python的字典变成可以穿越网络的符咒,又能把外来的数据包瞬间变成Python听得懂的情书。这就是我们今天要认识的明星模块——json
,它可是Python与外界数据交流的首席翻译官!
一、初识翻译官的绝技
1.1 基本对译法(序列化与反序列化)
先来看这个魔法咒语三件套:
import json
data = {
"name": "小明",
"age": 18,
"hobbies": ["coding", "篮球", "看动漫"]
}
# 序列化为JSON字符串(Python → JSON)
json_str = json.dumps(data)
print("基本序列化:", json_str)
# 输出:{"name": "\u5c0f\u660e", "age": 18, "hobbies": ["coding", "\u7bee\u7403", "\u770b\u52a8\u6f2b"]}
# 反序列化(JSON → Python)
new_data = json.loads(json_str)
print("反序列化后类型:", type(new_data)) # <class 'dict'>
等等!中文怎么变成乱码了?别急,给我们的翻译官配个翻译器:
# 解决中文乱码
json_str_chinese = json.dumps(data, ensure_ascii=False)
print("中文友好版:", json_str_chinese)
# 输出:{"name": "小明", "age": 18, "hobbies": ["coding", "篮球", "看动漫"]}
1.2 文件读写黑科技
想永久保存数据?看翻译官的文件操作:
# 写入JSON文件
with open("data.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False)
# 读取时自动还原
with open("data.json", encoding="utf-8") as f:
loaded_data = json.load(f)
print("文件读取:", loaded_data["hobbies"][1]) # 输出:篮球
二、驯服不听话的数据类型
2.1 处理datetime对象(对象闹脾气了)
当遇到datetime
这样的傲娇对象:
from datetime import datetime
event = {
"title": "Python见面会",
"time": datetime.now()
}
# 直接序列化会报错!
# json.dumps(event) # TypeError: Object of type datetime is not JSON serializable
我们需要定制翻译规则:
def custom_serializer(obj):
if isinstance(obj, datetime):
return obj.strftime("%Y-%m-%d %H:%M:%S")
raise TypeError("无法处理的类型")
json_str = json.dumps(event, default=custom_serializer, ensure_ascii=False)
print("处理后的时间:", json_str)
# 输出:{"title": "Python见面会", "time": "2023-08-20 14:30:00"}
2.2 复活自定义对象(对象转世重生)
假设我们有个萌宠类:
class Pet:
def __init__(self, name, species):
self.name = name
self.species = species
# 序列化定制
def pet_encoder(obj):
if isinstance(obj, Pet):
return {"name": obj.name, "species": obj.species}
raise TypeError
# 反序列化复活
def pet_decoder(dct):
if "species" in dct:
return Pet(dct["name"], dct["species"])
return dct
my_cat = Pet("橘座", "中华田园猫")
# 全套转换流程
json_str = json.dumps(my_cat, default=pet_encoder, ensure_ascii=False)
print("宠物序列化:", json_str) # {"name": "橘座", "species": "中华田园猫"}
new_pet = json.loads(json_str, object_hook=pet_decoder)
print(f"复活后的宠物:{new_pet.name}是{new_pet.species}") # 橘座是中华田园猫
三、翻译官的秘密武器
3.1 美化输出(强迫症福音)
data = {
"project": "Python大师课",
"students": ["张三", "李四", "王五"],
"scores": {"张三": 95, "李四": 88, "王五": 92}
}
# 缩进+ASCII控制+键排序
pretty_json = json.dumps(data,
indent=4,
ensure_ascii=False,
sort_keys=True)
print("精美排版版:")
print(pretty_json)
"""
{
"project": "Python大师课",
"scores": {
"张三": 95,
"李四": 88,
"王五": 92
},
"students": [
"张三",
"李四",
"王五"
]
}
"""
3.2 流式处理大文件(应对数据海啸)
当处理大型JSON文件时,内存友好型操作:
# 生成大型数据
big_data = [{"id": i, "value": i*10} for i in range(100000)]
# 增量写入
with open("big.json", "w") as f:
for item in big_data:
json.dump(item, f)
f.write('\n') # 换行分隔
# 流式读取
def process_big_file():
with open("big.json") as f:
for line in f:
yield json.loads(line)
print("处理第5万条数据:")
for idx, data in enumerate(process_big_file()):
if idx == 49999:
print(data) # {'id': 49999, 'value': 499990}
break
四、实战技巧宝典
4.1 解析网络数据(爬虫必备)
import requests
# 获取GitHub仓库信息
response = requests.get("https://api.github.com/repos/python/cpython")
repo_data = json.loads(response.text)
print(f"Python仓库:{repo_data['name']}")
print(f"星标数:{repo_data['stargazers_count']}")
print(f"开源协议:{repo_data['license']['name']}")
4.2 配置管理系统(开发神器)
# config.json
# {
# "database": {
# "host": "localhost",
# "port": 3306,
# "user": "admin"
# },
# "debug_mode": true
# }
class ConfigManager:
def __init__(self, file_path):
with open(file_path) as f:
self.config = json.load(f)
def get(self, key):
return self.config.get(key)
def update(self, key, value):
self.config[key] = value
with open(file_path, "w") as f:
json.dump(self.config, f, indent=4)
# 使用示例
config = ConfigManager("config.json")
print("数据库端口:", config.get("database")["port"]) # 3306
五、注意事项(避坑指南)
-
类型转换对照表
Python类型 JSON类型 dict object list array str string int/float number True/False true/false None null -
常见陷阱
- 集合(set)无法直接序列化
- 循环引用会导致无限递归
- 反序列化后的字典键都是字符串
- 默认不支持注释(虽然某些解析器可以容忍)
-
性能秘籍
- 对于超大数据考虑
ujson
(速度更快) - 使用
separators=(',', ':')
减小体积 - 避免频繁的序列化/反序列化操作
- 对于超大数据考虑
六、结语:让数据飞一会儿
现在你已经掌握了这位翻译官的所有绝活!下次当你的数据想要:
- 跳进网页和APP里玩耍
- 钻进数据库睡个觉
- 飞到其他服务器串门
记得请出我们的json模块,给它念段咒语(import json
),剩下的就交给这位可靠的数据信使吧!别忘了,实践出真知,赶紧打开你的Python编辑器,让数据开始它的奇幻漂流吧!