当前位置:首页>python>Python json模块深度解析:从基础到高级,构建健壮的数据交换系统

Python json模块深度解析:从基础到高级,构建健壮的数据交换系统

  • 2026-03-26 09:18:20
Python json模块深度解析:从基础到高级,构建健壮的数据交换系统
引言:JSON不只是字典转字符串那么简单!
在Python开发中,你是否经常遇到以下挑战:
  • 处理API数据时手忙脚乱:收到JSON响应却不知道如何优雅地转换为Python对象,代码中充斥着各种临时转换逻辑
  • 序列化自定义对象频频报错:尝试保存类实例到配置文件时,总是遇到TypeError: Object of type X is not JSON serializable
  • 性能瓶颈无法突破:处理大型JSON文件时内存溢出,或者序列化大量数据耗时过长
  • 安全隐患无处不在:从不可信来源解析JSON数据时,担忧JSON注入攻击或恶意负载
  • 数据验证缺失:缺少结构化的数据校验,运行时才发现数据类型不匹配或字段缺失
如果你正在面对这些困扰,那么深入掌握json模块就是你成为Python数据交换专家的必经之路!JSON作为Web时代的通用语言,几乎贯穿了现代应用开发的每一个环节。
今天,我们将从基础到高级,全面解析json模块的每一个细节,通过丰富的实战案例和代码示例,帮助你构建起完整的JSON处理知识体系,让你的代码更加健壮、高效和安全!
一、json模块概述
1.1 JSON的重要性与Python的实现
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,自2000年代起逐渐取代XML成为Web API的事实标准。它的优势显而易见:
  • 人类可读:相比二进制格式,JSON易于阅读和调试
  • 跨语言支持:几乎所有编程语言都有JSON库支持
  • 简洁紧凑:相比XML,JSON语法更加简洁
  • 无模式约束:灵活适应各种数据结构(虽然也有JSON Schema作为补充)
Python的json模块是标准库的一部分,实现了RFC 8259标准。它提供了Python对象与JSON字符串之间的双向转换,支持以下核心数据类型映射:
Python类型
JSON类型
转换规则
dict
object
键值对映射,键必须是字符串
listtuple
array
有序列表
str
string
Unicode字符序列
intfloat
number
整数或浮点数
True
true
布尔值真
False
false
布尔值假
None
null
空值
1.2 模块核心架构
json模块的架构围绕四个核心函数构建:
  • 序列化(Python → JSON)
    • dumps():将Python对象转换为JSON字符串
    • dump():将Python对象序列化并写入文件
  • 反序列化(JSON → Python)
    • loads():将JSON字符串解析为Python对象
    • load():从文件读取JSON数据并解析为Python对象
这个简洁的API设计背后,隐藏着强大的自定义能力和性能优化空间,接下来我们将逐一揭示。
二、基础操作:四核心函数深度解析
2.1 json.dumps():Python对象的JSON字符串转换
dumps()函数是使用最频繁的序列化函数,它将Python对象转换为JSON格式的字符串。
基础用法:
import json# 基本数据类型转换data = {"name""Alice","age"30,"is_student": False,"courses": ["Math""Physics""Computer Science"],"address": None,"score"95.5}json_str = json.dumps(data)print("基本序列化:")print(json_str)# 输出: {"name""Alice""age": 30, "is_student": false, "courses": ["Math""Physics""Computer Science"], "address": null, "score": 95.5}# 格式化输出(调试必备)pretty_json = json.dumps(data, indent=4, sort_keys=True)print("\n格式化输出:")print(pretty_json)# 输出:# {#     "address": null,#     "age": 30,#     "courses": [#         "Math",#         "Physics",#         "Computer Science"#     ],#     "is_student": false,#     "name""Alice",#     "score": 95.5# }# 中文处理chinese_data = {"name""张三""city""北京"}default_output = json.dumps(chinese_data)print("\n默认中文处理(转义):")print(default_output)  # {"name""\u5f20\u4e09""city""\u5317\u4eac"}correct_output = json.dumps(chinese_data, ensure_ascii=False)print("\n正确中文处理:")print(correct_output)  # {"name""张三""city""北京"}
关键参数详解:
  • indent:缩进空格数(或字符串),用于美化输出
  • sort_keys:按键排序,确保序列化结果确定性
  • ensure_ascii:控制非ASCII字符处理,False时保留原字符
  • separators:自定义分隔符,如(',', ':')可生成最紧凑JSON
2.2 json.loads():JSON字符串的Python对象解析
loads()函数将JSON字符串解析为Python对象,是dumps()的逆操作。
基础用法:
import json# 字符串解析json_str = '{"name": "Bob", "age": 25, "active": true, "tags": ["python", "web"]}'data = json.loads(json_str)print("解析结果:")print(f"类型: {type(data)}")  # <class 'dict'>print(f"内容: {data}")print(f"姓名: {data['name']}")  # Bobprint(f"标签: {data['tags'][0]}")  # python# 错误处理invalid_json = '{"name": "Charlie", age: 30}'# 键缺少引号try:    result = json.loads(invalid_json)except json.JSONDecodeError as e:    print(f"\nJSON解析错误:")    print(f"错误信息: {e.msg}")    print(f"错误位置: 第{e.lineno}行, 第{e.colno}列")    print(f"错误片段: {e.doc[e.pos:e.pos+20]}")
2.3 json.dump()与json.load():文件操作
对于文件I/O场景,dump()load()提供了更直接的接口。
文件序列化与反序列化:
import jsonimport os# 准备测试数据data = {"project""JSON Tutorial","version""1.0","author""Python与AI智能研习社","modules": ["json""csv""yaml"],"config": {"debug"True,"port"8080,"host""localhost"    }}# 写入JSON文件with open('config.json''w', encoding='utf-8'as f:    json.dump(data, f, ensure_ascii=False, indent=2)print("文件已写入: config.json")print(f"文件大小: {os.path.getsize('config.json')} 字节")# 读取JSON文件with open('config.json''r', encoding='utf-8'as f:    loaded_data = json.load(f)print("\n读取验证:")print(f"项目名称: {loaded_data['project']}")print(f"作者: {loaded_data['author']}")print(f"模块数量: {len(loaded_data['modules'])}")# 清理测试文件os.remove('config.json')
三、高级功能:自定义序列化与反序列化
3.1 处理自定义类实例
Python的类实例无法直接序列化为JSON,需要自定义转换逻辑。
方法一:使用default参数
import jsonfrom datetime import datetimefrom decimal import DecimalclassUser:"""自定义用户类"""def__init__(self, name, email, created_at, balance):        self.name = name        self.email = email        self.created_at = created_at  # datetime对象        self.balance = balance  # Decimal对象def__repr__(self):returnf"User(name={self.name!r}, email={self.email!r})"defcustom_encoder(obj):"""自定义编码器函数"""if isinstance(obj, datetime):# 将datetime转换为ISO格式字符串return obj.isoformat()elif isinstance(obj, Decimal):# 将Decimal转换为字符串,避免精度丢失return str(obj)elif isinstance(obj, User):# 将User实例转换为字典return {"name": obj.name,"email": obj.email,"created_at": obj.created_at,"balance": obj.balance,"_type""User"# 添加类型标识,便于反序列化        }# 无法处理的类型,让基类处理raise TypeError(f"Object of type {type(obj).__name__} is not JSON serializable")# 创建测试对象user = User(    name="李四",    email="lisi@example.com",    created_at=datetime(202431714300),    balance=Decimal("1234.56"))# 序列化自定义对象user_json = json.dumps(user, default=custom_encoder, ensure_ascii=False, indent=2)print("自定义对象序列化结果:")print(user_json)# 输出:# {#   "name": "李四",#   "email": "lisi@example.com",#   "created_at": "2024-03-17T14:30:00",#   "balance": "1234.56",#   "_type": "User"# }
方法二:继承JSONEncoder类
import jsonfrom datetime import datetimefrom decimal import DecimalclassCustomJSONEncoder(json.JSONEncoder):"""自定义JSON编码器"""defdefault(self, obj):"""重写default方法"""if isinstance(obj, datetime):return {"_type""datetime","value": obj.isoformat()            }elif isinstance(obj, Decimal):return {"_type""decimal","value": str(obj)            }elif hasattr(obj, '__dict__'):# 处理任意自定义对象(有__dict__属性)            result = obj.__dict__.copy()            result['_type'] = type(obj).__name__return result# 调用父类方法处理其他类型return super().default(obj)# 使用自定义编码器encoder = CustomJSONEncoder(indent=2, ensure_ascii=False)encoded = encoder.encode(user)print("\n使用CustomJSONEncoder:")print(encoded)
3.2 自定义反序列化:还原复杂对象
解析JSON时,我们可以将特定的字典结构还原为Python对象。
使用object_hook参数:
defcustom_decoder(dct):"""自定义解码器函数"""# 检查类型标识if'_type'in dct:        type_name = dct['_type']if type_name == 'User':# 还原User对象return User(                name=dct['name'],                email=dct['email'],                created_at=datetime.fromisoformat(dct['created_at']),                balance=Decimal(dct['balance'])            )elif type_name == 'datetime':# 还原datetime对象return datetime.fromisoformat(dct['value'])elif type_name == 'decimal':# 还原Decimal对象return Decimal(dct['value'])# 没有类型标识,返回原字典return dct# 测试反序列化decoded_user = json.loads(user_json, object_hook=custom_decoder)print("\n反序列化结果验证:")print(f"类型: {type(decoded_user)}")  # <class '__main__.User'>print(f"姓名: {decoded_user.name}")  # 李四print(f"余额类型: {type(decoded_user.balance)}")  # <class 'decimal.Decimal'>print(f"创建时间: {decoded_user.created_at}")
3.3 更精细的控制:object_pairs_hook
对于需要保留键值对顺序的场景,可以使用object_pairs_hook
from collections import OrderedDict# 保留原始键值对顺序ordered_json = '{"z": 1, "a": 2, "c": 3}'# 使用object_pairs_hook保留顺序data = json.loads(ordered_json, object_pairs_hook=OrderedDict)print("保留顺序的解析:")for key, value in data.items():    print(f"{key}{value}")# 输出顺序: z, a, c (与JSON中顺序一致)# 对比默认解析(Python 3.7+字典保持插入顺序,但不保证与JSON一致)default_data = json.loads(ordered_json)print("\n默认解析(Python 3.7+保持插入顺序):")for key, value in default_data.items():    print(f"{key}{value}")
四、性能优化技巧
4.1 大型JSON文件的处理
处理大型JSON文件时,内存占用是首要考虑的问题。
流式解析:
import jsonimport ijson  # 需要安装: pip install ijsondefprocess_large_json_file(file_path):"""流式处理大型JSON文件"""with open(file_path, 'r', encoding='utf-8'as f:# 使用ijson进行流式解析        objects = ijson.items(f, 'item')  # 假设JSON数组的键是'item'for obj in objects:# 逐个处理对象,避免一次性加载到内存            process_item(obj)    print("流式处理完成")defprocess_item(item):"""处理单个数据项(示例)"""# 这里是业务处理逻辑pass# 传统方法(内存危险):# with open('large.json', 'r') as f:#     data = json.load(f)  # 可能内存溢出
分批读写:
defbatch_process_json(input_file, output_file, batch_size=1000):"""分批处理JSON数据"""with open(input_file, 'r', encoding='utf-8'as f_in:# 逐行读取(假设每行是一个JSON对象)        batch = []for line_num, line in enumerate(f_in, 1):try:                obj = json.loads(line.strip())                batch.append(obj)# 达到批次大小时处理if len(batch) >= batch_size:                    process_batch(batch, line_num - batch_size, line_num)                    batch.clear()except json.JSONDecodeError as e:                print(f"第{line_num}行解析错误: {e}")# 处理剩余数据if batch:            process_batch(batch, line_num - len(batch) + 1, line_num)    print("分批处理完成")defprocess_batch(batch, start_line, end_line):"""处理一个批次的数据"""    print(f"处理批次: 第{start_line}-{end_line}行,共{len(batch)}条记录")# 这里实现具体的业务逻辑
4.2 使用高性能第三方库
标准库json模块性能良好,但在极端性能要求场景下,可以考虑第三方库。
orjson(最快的JSON库):
# 安装: pip install orjsonimport orjsonimport timedata = {"name""性能测试","values": [i for i in range(100000)],"nested": {"level1": {"level2": {"level3""deep"}}}}# 标准库性能start = time.time()json_str = json.dumps(data)json_time = time.time() - start# orjson性能start = time.time()orjson_str = orjson.dumps(data).decode('utf-8')orjson_time = time.time() - startprint(f"标准库序列化时间: {json_time:.4f}秒")print(f"orjson序列化时间: {orjson_time:.4f}秒")print(f"性能提升: {(json_time/orjson_time):.1f}倍")# orjson特性:# 1. 默认输出bytes而不是str# 2. 支持datetime、UUID、numpy数组等# 3. 不支持自定义编码器(设计取舍)
ujson(兼顾性能与兼容性):
# 安装: pip install ujsonimport ujson# 使用方式与标准库基本相同fast_json = ujson.dumps(data)fast_data = ujson.loads(fast_json)# ujson优势:# 1. 比标准库快2-3倍# 2. API与标准库高度兼容# 3. 支持大整数不溢出
4.3 内存优化技巧
使用生成器减少内存占用:
defgenerate_large_json_stream(items):"""生成JSON数据流(不一次性构建完整JSON)"""yield'['for i, item in enumerate(items):if i > 0:yield','yield json.dumps(item)yield']'# 使用示例items = [{"id": i, "data""value" + str(i)} for i in range(1000000)]with open('large_output.json''w', encoding='utf-8'as f:for chunk in generate_large_json_stream(items):        f.write(chunk)    print("流式写入完成,内存占用恒定")
选择性序列化:
defselective_serialize(obj, include_fields=None, exclude_fields=None):"""选择性序列化,只包含需要的字段"""if include_fields isnotNone:# 只包含指定字段        filtered = {k: v for k, v in obj.items() if k in include_fields}return json.dumps(filtered)elif exclude_fields isnotNone:# 排除指定字段        filtered = {k: v for k, v in obj.items() if k notin exclude_fields}return json.dumps(filtered)else:# 包含所有字段return json.dumps(obj)# 使用示例user_data = {"id"123,"name""王五","email""wangwu@example.com","password_hash""abc123","created_at""2024-03-17","last_login""2024-03-17 14:30:00"}# 只包含公开字段(排除敏感信息)public_json = selective_serialize(    user_data,     include_fields=["id""name""email""created_at"])print("安全序列化结果:")print(public_json)# {"id": 123, "name": "王五", "email": "wangwu@example.com", "created_at": "2024-03-17"}
五、安全最佳实践
5.1 防御JSON注入攻击
JSON注入是Web应用中常见的安全漏洞。
危险示例:
# 危险:直接拼接用户输入user_input = '{"malicious": true}'dangerous_json = '{"data": ' + user_input + '}'# 可能被注入# 安全解析示例defsafe_json_parse(input_str, default=None):"""安全解析JSON,防止注入"""try:return json.loads(input_str)except (json.JSONDecodeError, TypeError):return default# 使用示例user_provided = '{"name": "test", "extra": "malicious"}}'# 多了一个大括号result = safe_json_parse(user_provided, default={"error""invalid"})print(f"安全解析结果: {result}")
5.2 处理不可信来源的JSON
从外部API或用户输入获取JSON时,必须进行安全验证。
使用严格模式:
import jsonimport demjson3  # 需要安装: pip install demjson3defvalidate_and_parse_strict(json_str):"""严格验证和解析JSON"""try:# 使用demjson3进行严格验证return demjson3.decode(json_str, strict=True)except demjson3.JSONDecodeError as e:        print(f"严格模式验证失败: {e}")# 记录日志,返回安全默认值returnNone# 测试严格模式test_cases = ['{"valid": true}',  # 有效'{"invalid": 0123}',  # 无效:数字有前导零'{"duplicate": 1, "duplicate": 2}',  # 无效:重复键'{"trailing": "comma",}',  # 无效:尾部逗号]for i, test in enumerate(test_cases, 1):    result = validate_and_parse_strict(test)    print(f"测试{i}{test[:30]}... -> {'有效'if result else'无效'}")
5.3 防止资源耗尽攻击
恶意用户可能发送超大或深度嵌套的JSON进行攻击。
设置解析限制:
classSafeJSONDecoder(json.JSONDecoder):"""安全的JSON解码器,防止资源耗尽"""def__init__(self, *args, max_depth=100, max_str_len=10**7, **kwargs):        self.max_depth = max_depth        self.max_str_len = max_str_len        super().__init__(*args, **kwargs)defdecode(self, s, **kwargs):# 检查字符串长度if len(s) > self.max_str_len:raise ValueError(f"JSON字符串过长({len(s)} > {self.max_str_len})")# 调用父类解析,并在解析过程中检查深度return super().decode(s, **kwargs)# 使用安全解码器safe_decoder = SafeJSONDecoder(max_depth=50, max_str_len=10**6)try:# 尝试解析可能深度嵌套的JSON    deep_json = '{' * 100 + '"key": "value"' + '}' * 100    result = json.loads(deep_json, cls=safe_decoder)except (ValueError, RecursionError) as e:    print(f"安全检查拦截: {e}")
六、实战应用场景
6.1 场景一:RESTful API开发
JSON是RESTful API的事实标准格式。
API请求与响应处理:
import jsonimport requestsfrom typing import Dict, AnyclassAPIClient:"""RESTful API客户端"""def__init__(self, base_url: str, timeout: int = 30):        self.base_url = base_url        self.timeout = timeout        self.session = requests.Session()defget(self, endpoint: str, params: Dict = None) -> Dict[str, Any]:"""发送GET请求"""        url = f"{self.base_url}/{endpoint}"try:            response = self.session.get(                url,                 params=params,                 timeout=self.timeout            )            response.raise_for_status()# 自动解析JSON响应return response.json()except requests.exceptions.RequestException as e:            print(f"API请求失败: {e}")return {"error": str(e), "success"False}defpost(self, endpoint: str, data: Dict) -> Dict[str, Any]:"""发送POST请求(JSON格式)"""        url = f"{self.base_url}/{endpoint}"try:            response = self.session.post(                url,                json=data,  # requests自动序列化为JSON                timeout=self.timeout            )            response.raise_for_status()return response.json()except requests.exceptions.RequestException as e:            print(f"API请求失败: {e}")return {"error": str(e), "success"False}defpost_with_custom_serialization(self, endpoint: str, obj) -> Dict[str, Any]:"""发送POST请求(自定义序列化)"""        url = f"{self.base_url}/{endpoint}"# 自定义序列化        json_str = json.dumps(obj, default=custom_encoder, ensure_ascii=False)try:            response = self.session.post(                url,                data=json_str,                headers={'Content-Type''application/json'},                timeout=self.timeout            )            response.raise_for_status()return response.json()except requests.exceptions.RequestException as e:            print(f"API请求失败: {e}")return {"error": str(e), "success"False}# 使用示例# client = APIClient("https://api.example.com")# user_data = client.get("users/123")# new_user = client.post("users", {"name": "新用户", "email": "new@example.com"})
6.2 场景二:配置文件管理
JSON非常适合用于应用配置。
配置管理系统:
import jsonimport osfrom typing import Dict, Anyfrom pathlib import PathclassConfigManager:"""JSON配置文件管理器"""def__init__(self, config_path: str = "config.json"):        self.config_path = Path(config_path)        self.config = self.load_config()defload_config(self, default_config: Dict = None) -> Dict[str, Any]:"""加载配置文件"""ifnot self.config_path.exists():            print(f"配置文件不存在: {self.config_path}")if default_config:                self.save_config(default_config)return default_configelse:return {}try:with open(self.config_path, 'r', encoding='utf-8'as f:                config = json.load(f)                print(f"配置文件加载成功: {self.config_path}")return configexcept (json.JSONDecodeError, OSError) as e:            print(f"配置文件解析失败: {e}")if default_config:                print("使用默认配置")return default_configelse:return {}defsave_config(self, config: Dict = None) -> bool:"""保存配置文件"""if config isNone:            config = self.configtry:# 创建目录(如果需要)            self.config_path.parent.mkdir(parents=True, exist_ok=True)with open(self.config_path, 'w', encoding='utf-8'as f:                json.dump(config, f, ensure_ascii=False, indent=2)            print(f"配置文件保存成功: {self.config_path}")returnTrueexcept OSError as e:            print(f"配置文件保存失败: {e}")returnFalsedefget(self, key: str, default=None) -> Any:"""获取配置值"""return self.config.get(key, default)defset(self, key: str, value: Any, save: bool = True) -> None:"""设置配置值"""        self.config[key] = valueif save:            self.save_config()defupdate(self, updates: Dict, save: bool = True) -> None:"""批量更新配置"""        self.config.update(updates)if save:            self.save_config()# 使用示例default_config = {"app": {"name""MyApp","version""1.0.0","debug"True    },"database": {"host""localhost","port"5432,"name""mydb"    },"server": {"host""0.0.0.0","port"8080    }}config_manager = ConfigManager("myapp/config.json")app_name = config_manager.get("app.name""默认应用")print(f"应用名称: {app_name}")# 更新配置config_manager.set("app.debug"False)config_manager.update({"server": {"port"9000}})
6.3 场景三:数据交换与日志记录
JSON在数据交换和结构化日志中扮演关键角色。
结构化日志系统:
import jsonimport loggingimport sysfrom datetime import datetimefrom typing import Dict, AnyclassJSONFormatter(logging.Formatter):"""JSON格式的日志格式化器"""defformat(self, record: logging.LogRecord) -> str:"""格式化日志记录为JSON"""        log_data = {"timestamp": datetime.utcnow().isoformat() + "Z","level": record.levelname,"logger": record.name,"message": record.getMessage(),"module": record.module,"function": record.funcName,"line": record.lineno        }# 添加额外字段if hasattr(record, 'extra'):            log_data.update(record.extra)# 处理异常if record.exc_info:            log_data['exception'] = self.formatException(record.exc_info)return json.dumps(log_data, ensure_ascii=False)defformatException(self, exc_info) -> Dict[str, Any]:"""格式化异常信息"""        ex_type, ex_value, ex_traceback = exc_inforeturn {"type": ex_type.__name__,"message": str(ex_value),"traceback": self.format_traceback(ex_traceback)        }defformat_traceback(self, traceback) -> list:"""格式化跟踪栈"""# 这里简化处理,实际可以提取更多信息import traceback as tbreturn tb.format_tb(traceback)# 配置日志系统defsetup_logging():"""设置JSON格式的日志"""    logger = logging.getLogger()    logger.setLevel(logging.INFO)# 移除现有处理器    logger.handlers.clear()# 控制台输出(JSON格式)    console_handler = logging.StreamHandler(sys.stdout)    console_handler.setFormatter(JSONFormatter())    logger.addHandler(console_handler)# 文件输出(JSON Lines格式)    file_handler = logging.FileHandler("app.log", encoding='utf-8')    file_handler.setFormatter(JSONFormatter())    logger.addHandler(file_handler)return logger# 使用示例logger = setup_logging()# 普通日志logger.info("应用启动成功")# 带上下文的日志logger.info("用户登录", extra={"user_id"12345,"username""zhangsan","ip_address""192.168.1.100"})# 错误日志try:    result = 1 / 0except ZeroDivisionError:    logger.error("除零错误", exc_info=True)
七、常见问题与解决方案
7.1 Q:如何处理JSON中的日期时间格式?
A: JSON没有日期时间类型,需要手动转换。推荐使用ISO 8601格式。
import jsonfrom datetime import datetimefrom dateutil import parser  # 需要安装: pip install python-dateutil# 序列化defserialize_datetime(obj):if isinstance(obj, datetime):return obj.isoformat()raise TypeError(f"Type {type(obj)} not serializable")# 反序列化defdeserialize_datetime(dct):for key, value in dct.items():# 尝试解析ISO格式的日期时间字符串if isinstance(value, str):try:# 尝试解析多种格式                dct[key] = parser.isoparse(value)except (ValueError, TypeError):# 不是日期时间字符串,保持原样passreturn dct# 使用示例data = {"event""meeting""time": datetime.now()}json_str = json.dumps(data, default=serialize_datetime)print(f"序列化: {json_str}")parsed = json.loads(json_str, object_hook=deserialize_datetime)print(f"反序列化时间类型: {type(parsed['time'])}")
7.2 Q:如何优化大型JSON数组的序列化性能?
A: 使用生成器和流式处理,避免一次性构建完整数据结构。
defgenerate_large_data_stream(count):"""生成大型数据流"""for i in range(count):yield {"id": i,"data"f"item_{i}","timestamp": datetime.now().isoformat()        }defsave_json_stream(stream, file_path):"""流式保存JSON数据"""with open(file_path, 'w', encoding='utf-8'as f:        f.write('[\n')        first = Truefor item in stream:ifnot first:                f.write(',\n')            json_line = json.dumps(item, ensure_ascii=False)            f.write('  ' + json_line)            first = False        f.write('\n]')    print(f"流式保存完成: {file_path}")# 使用示例(生成100万条记录)# data_stream = generate_large_data_stream(1000000)# save_json_stream(data_stream, "large_data.json")
7.3 Q:如何验证JSON数据的结构?
A: 使用JSON Schema进行结构化验证。
import jsonimport jsonschema  # 需要安装: pip install jsonschema# 定义Schemauser_schema = {"type""object","properties": {"name": {"type""string""minLength"1},"age": {"type""integer""minimum"0"maximum"150},"email": {"type""string""format""email"},"is_active": {"type""boolean"},"tags": {"type""array","items": {"type""string"},"uniqueItems"True        }    },"required": ["name""email"],"additionalProperties"False}defvalidate_user_data(data):"""验证用户数据是否符合Schema"""try:        jsonschema.validate(instance=data, schema=user_schema)returnTrue"验证通过"except jsonschema.ValidationError as e:returnFalsef"验证失败: {e.message}"except json.JSONDecodeError as e:returnFalsef"JSON解析失败: {e}"# 测试验证test_cases = [    {"name""张三""email""zhangsan@example.com""age"25},  # 有效    {"name""""email""invalid"},  # 无效:姓名为空,邮箱格式错误    {"name""李四""age""二十五"},  # 无效:年龄不是整数]for i, test_data in enumerate(test_cases, 1):    is_valid, message = validate_user_data(test_data)    print(f"测试{i}{test_data.get('name''无名')} -> {message}")
八、总结与延伸学习
8.1 核心要点回顾
通过今天的深度解析,我们全面掌握了json模块的核心功能:
  1. 基础操作dumps()/loads()用于字符串转换,dump()/load()用于文件操作
  2. 高级功能:自定义编码器/解码器处理复杂对象,使用object_hook和object_pairs_hook实现精细控制
  3. 性能优化:流式处理大型文件,使用高性能第三方库,选择性序列化减少数据量
  4. 安全实践:防御JSON注入攻击,验证不可信来源,防止资源耗尽
  5. 实战应用:RESTful API开发、配置文件管理、数据交换与日志记录
8.2 最佳实践清单
  • 始终验证输入:来自不可信来源的JSON必须经过严格验证
  • 使用适当的数据类型:对于高精度数值,使用Decimal而不是float
  • 设置合理的限制:防止恶意的大尺寸或深度嵌套JSON
  • 考虑性能与兼容性的平衡:标准库足够大多数场景,极端场景考虑orjson/ujson
  • 保持向后兼容:修改JSON结构时考虑旧版本兼容性
  • 使用JSON Schema:对于复杂数据结构,使用Schema进行定义和验证
  • 结构化日志:使用JSON格式记录日志,便于后续分析和监控
8.3 延伸学习路径
json模块只是Python数据交换生态的一部分,要继续深入:
  1. 相关模块
    • csv:处理表格数据
    • xml.etree.ElementTree:处理XML数据
    • yaml:处理YAML格式(需要第三方库)
  2. 第三方库
    • pydantic:数据验证和设置管理
    • marshmallow:对象序列化/反序列化框架
    • dataclasses-json:为数据类提供JSON支持
  3. 性能优化
    • 学习使用ijson处理超大JSON文件
    • 掌握orjsonujson的性能特性
    • 了解Python的序列化协议(pickle、msgpack等)
  4. 系统设计
    • 分布式系统中的数据交换协议
    • 微服务间的API设计原则
    • 数据版本管理和迁移策略
8.4 最后的建议
JSON作为现代应用开发的通用语言,掌握json模块是每个Python开发者的基本素养。但更重要的是理解背后的设计思想:
  • 简单性优先:JSON的设计哲学是简单易用,你的实现也应该遵循这一原则
  • 安全性不容妥协:数据交换中的安全漏洞可能导致严重后果
  • 性能需要实测:不同场景下的性能表现差异巨大,需要实际测试
  • 可维护性是关键:复杂的自定义编码器可能成为技术债务
记住:技术服务于业务,而不是相反。选择最简单、最安全的解决方案,在必要时才引入复杂性。

下一篇预告:明天我们将探索re模块,学习正则表达式在文本处理中的强大应用,从基础语法到高级技巧全面解析。敬请期待!

本文为"Python与AI智能研习社"公众号原创文章,转载请注明出处。关注公众号,获取更多Python技术干货!

最新文章

随机文章

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-03-27 10:06:06 HTTP/2.0 GET : https://f.mffb.com.cn/a/482843.html
  2. 运行时间 : 0.239627s [ 吞吐率:4.17req/s ] 内存消耗:4,758.91kb 文件加载:140
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=bd97e8b1e477c4588139fdb26dd379cb
  1. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/public/index.php ( 0.79 KB )
  2. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/autoload.php ( 0.17 KB )
  3. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_real.php ( 2.49 KB )
  4. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/platform_check.php ( 0.90 KB )
  5. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/ClassLoader.php ( 14.03 KB )
  6. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_static.php ( 4.90 KB )
  7. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper.php ( 8.34 KB )
  8. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/helper.php ( 2.19 KB )
  9. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/helper.php ( 1.47 KB )
  10. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/stubs/load_stubs.php ( 0.16 KB )
  11. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Exception.php ( 1.69 KB )
  12. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Facade.php ( 2.71 KB )
  13. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/deprecation-contracts/function.php ( 0.99 KB )
  14. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap.php ( 8.26 KB )
  15. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap80.php ( 9.78 KB )
  16. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/Resources/functions/dump.php ( 1.49 KB )
  17. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-dumper/src/helper.php ( 0.18 KB )
  18. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/VarDumper.php ( 4.30 KB )
  19. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/App.php ( 15.30 KB )
  20. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Container.php ( 15.76 KB )
  21. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/container/src/ContainerInterface.php ( 1.02 KB )
  22. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/provider.php ( 0.19 KB )
  23. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Http.php ( 6.04 KB )
  24. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Str.php ( 7.29 KB )
  25. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Env.php ( 4.68 KB )
  26. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/common.php ( 0.03 KB )
  27. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/helper.php ( 18.78 KB )
  28. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Config.php ( 5.54 KB )
  29. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/app.php ( 0.95 KB )
  30. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cache.php ( 0.78 KB )
  31. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/console.php ( 0.23 KB )
  32. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cookie.php ( 0.56 KB )
  33. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/database.php ( 2.48 KB )
  34. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Env.php ( 1.67 KB )
  35. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/filesystem.php ( 0.61 KB )
  36. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/lang.php ( 0.91 KB )
  37. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/log.php ( 1.35 KB )
  38. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/middleware.php ( 0.19 KB )
  39. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/route.php ( 1.89 KB )
  40. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/session.php ( 0.57 KB )
  41. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/trace.php ( 0.34 KB )
  42. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/view.php ( 0.82 KB )
  43. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/event.php ( 0.25 KB )
  44. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Event.php ( 7.67 KB )
  45. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/service.php ( 0.13 KB )
  46. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/AppService.php ( 0.26 KB )
  47. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Service.php ( 1.64 KB )
  48. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Lang.php ( 7.35 KB )
  49. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/lang/zh-cn.php ( 13.70 KB )
  50. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/Error.php ( 3.31 KB )
  51. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/RegisterService.php ( 1.33 KB )
  52. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/services.php ( 0.14 KB )
  53. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/PaginatorService.php ( 1.52 KB )
  54. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ValidateService.php ( 0.99 KB )
  55. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ModelService.php ( 2.04 KB )
  56. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Service.php ( 0.77 KB )
  57. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Middleware.php ( 6.72 KB )
  58. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/BootService.php ( 0.77 KB )
  59. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Paginator.php ( 11.86 KB )
  60. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/Validate.php ( 63.20 KB )
  61. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Model.php ( 23.55 KB )
  62. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Attribute.php ( 21.05 KB )
  63. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/AutoWriteData.php ( 4.21 KB )
  64. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Conversion.php ( 6.44 KB )
  65. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/DbConnect.php ( 5.16 KB )
  66. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/ModelEvent.php ( 2.33 KB )
  67. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/RelationShip.php ( 28.29 KB )
  68. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Arrayable.php ( 0.09 KB )
  69. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Jsonable.php ( 0.13 KB )
  70. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/contract/Modelable.php ( 0.09 KB )
  71. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Db.php ( 2.88 KB )
  72. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/DbManager.php ( 8.52 KB )
  73. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Log.php ( 6.28 KB )
  74. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Manager.php ( 3.92 KB )
  75. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerTrait.php ( 2.69 KB )
  76. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerInterface.php ( 2.71 KB )
  77. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cache.php ( 4.92 KB )
  78. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/simple-cache/src/CacheInterface.php ( 4.71 KB )
  79. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Arr.php ( 16.63 KB )
  80. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/driver/File.php ( 7.84 KB )
  81. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/Driver.php ( 9.03 KB )
  82. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/CacheHandlerInterface.php ( 1.99 KB )
  83. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/Request.php ( 0.09 KB )
  84. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Request.php ( 55.78 KB )
  85. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/middleware.php ( 0.25 KB )
  86. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Pipeline.php ( 2.61 KB )
  87. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/TraceDebug.php ( 3.40 KB )
  88. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/middleware/SessionInit.php ( 1.94 KB )
  89. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Session.php ( 1.80 KB )
  90. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/driver/File.php ( 6.27 KB )
  91. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/SessionHandlerInterface.php ( 0.87 KB )
  92. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/Store.php ( 7.12 KB )
  93. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Route.php ( 23.73 KB )
  94. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleName.php ( 5.75 KB )
  95. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Domain.php ( 2.53 KB )
  96. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleGroup.php ( 22.43 KB )
  97. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Rule.php ( 26.95 KB )
  98. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleItem.php ( 9.78 KB )
  99. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/route/app.php ( 1.72 KB )
  100. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Route.php ( 4.70 KB )
  101. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/dispatch/Controller.php ( 4.74 KB )
  102. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Dispatch.php ( 10.44 KB )
  103. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/controller/Index.php ( 4.81 KB )
  104. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/BaseController.php ( 2.05 KB )
  105. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/facade/Db.php ( 0.93 KB )
  106. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/connector/Mysql.php ( 5.44 KB )
  107. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/PDOConnection.php ( 52.47 KB )
  108. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Connection.php ( 8.39 KB )
  109. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/ConnectionInterface.php ( 4.57 KB )
  110. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/builder/Mysql.php ( 16.58 KB )
  111. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Builder.php ( 24.06 KB )
  112. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseBuilder.php ( 27.50 KB )
  113. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Query.php ( 15.71 KB )
  114. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseQuery.php ( 45.13 KB )
  115. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TimeFieldQuery.php ( 7.43 KB )
  116. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/AggregateQuery.php ( 3.26 KB )
  117. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ModelRelationQuery.php ( 20.07 KB )
  118. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ParamsBind.php ( 3.66 KB )
  119. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ResultOperation.php ( 7.01 KB )
  120. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/WhereQuery.php ( 19.37 KB )
  121. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/JoinAndViewQuery.php ( 7.11 KB )
  122. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TableFieldInfo.php ( 2.63 KB )
  123. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/Transaction.php ( 2.77 KB )
  124. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/driver/File.php ( 5.96 KB )
  125. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/LogHandlerInterface.php ( 0.86 KB )
  126. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/Channel.php ( 3.89 KB )
  127. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/event/LogRecord.php ( 1.02 KB )
  128. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/Collection.php ( 16.47 KB )
  129. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/View.php ( 1.70 KB )
  130. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/View.php ( 4.39 KB )
  131. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Response.php ( 8.81 KB )
  132. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/response/View.php ( 3.29 KB )
  133. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cookie.php ( 6.06 KB )
  134. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-view/src/Think.php ( 8.38 KB )
  135. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/TemplateHandlerInterface.php ( 1.60 KB )
  136. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/Template.php ( 46.61 KB )
  137. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/driver/File.php ( 2.41 KB )
  138. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/contract/DriverInterface.php ( 0.86 KB )
  139. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/runtime/temp/067d451b9a0c665040f3f1bdd3293d68.php ( 11.98 KB )
  140. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Html.php ( 4.42 KB )
  1. CONNECT:[ UseTime:0.000880s ] mysql:host=127.0.0.1;port=3306;dbname=f_mffb;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `fenlei` [ RunTime:0.000799s ]
  3. SELECT * FROM `fenlei` WHERE `fid` = 0 [ RunTime:0.000523s ]
  4. SELECT * FROM `fenlei` WHERE `fid` = 63 [ RunTime:0.000315s ]
  5. SHOW FULL COLUMNS FROM `set` [ RunTime:0.000481s ]
  6. SELECT * FROM `set` [ RunTime:0.013880s ]
  7. SHOW FULL COLUMNS FROM `article` [ RunTime:0.000652s ]
  8. SELECT * FROM `article` WHERE `id` = 482843 LIMIT 1 [ RunTime:0.006589s ]
  9. UPDATE `article` SET `lasttime` = 1774577166 WHERE `id` = 482843 [ RunTime:0.016093s ]
  10. SELECT * FROM `fenlei` WHERE `id` = 66 LIMIT 1 [ RunTime:0.005702s ]
  11. SELECT * FROM `article` WHERE `id` < 482843 ORDER BY `id` DESC LIMIT 1 [ RunTime:0.005345s ]
  12. SELECT * FROM `article` WHERE `id` > 482843 ORDER BY `id` ASC LIMIT 1 [ RunTime:0.000469s ]
  13. SELECT * FROM `article` WHERE `id` < 482843 ORDER BY `id` DESC LIMIT 10 [ RunTime:0.000913s ]
  14. SELECT * FROM `article` WHERE `id` < 482843 ORDER BY `id` DESC LIMIT 10,10 [ RunTime:0.007093s ]
  15. SELECT * FROM `article` WHERE `id` < 482843 ORDER BY `id` DESC LIMIT 20,10 [ RunTime:0.004988s ]
0.245346s