JSON文件格式及Python操作详解
JSON(JavaScript Object Notation)是一种轻量级、易于阅读和编写的文本数据交换格式,广泛用于Web应用、API数据传输和配置文件存储。本文将详细介绍JSON的结构,并通过Python标准库json模块,演示如何实现JSON的读取、写入、查询与修改。
一、JSON基础语法
1. JSON数据结构
JSON支持以下数据类型:
- 对象(Object):键值对的集合,用{}包裹,键必须为字符串且用双引号括起。
- 数组(Array):有序的值列表,用[]包裹。
- 字符串(String):用双引号括起。
- 数值(Number):整数或浮点数。
- 布尔值(Boolean):true或false(注意全小写)。
- 空值(Null):null。
{
"name": "Alice",
"age": 30,
"is_student": false,
"scores": [90, 85, 95],
"address": {
"city": "Beijing",
"zipcode": "100000"
}
}
2. JSON与XML的对比
- 轻量级:JSON比XML结构更简洁,体积更小。
- 易读性:JSON使用键值对,更直观。
- 跨语言支持:JSON是语言无关的,支持多种编程语言解析。
二、Python中操作JSON的工具:json模块
Python标准库的json模块提供了对JSON数据的解析和生成功能,无需额外安装。
1. 安装与导入
import json # 直接导入即可
三、JSON的读取(解析)
1. 从字符串解析(json.loads())
将JSON格式的字符串转换为Python对象(字典或列表)。
json_str = '{"name": "Bob", "age": 25}'
data = json.loads(json_str)
print(type(data)) # 输出: <class 'dict'>
print(data["name"]) # 输出: Bob
2. 从文件读取(json.load())
读取JSON文件内容并转换为Python对象。
with open("data.json", "r", encoding="utf-8") as f:
data = json.load(f)
print(data["address"]["city"]) # 输出: Beijing
四、JSON的写入(序列化)
1. 将Python对象转为JSON字符串(json.dumps())
data = {
"name": "Charlie",
"hobbies": ["reading", "coding"]
}
json_str = json.dumps(data, indent=2) # indent参数使输出更易读
print(json_str)
# 输出:
# {
# "name": "Charlie",
# "hobbies": ["reading", "coding"]
# }
2. 写入JSON文件(json.dump())
with open("output.json", "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
五、JSON数据的查询与访问
1. 访问嵌套数据
data = {
"user": {
"name": "David",
"contacts": {
"email": "david@example.com",
"phone": "123456789"
}
}
}
# 访问嵌套键
print(data["user"]["contacts"]["email"]) # 输出: david@example.com
2. 遍历数组
scores = [90, 85, 95]
for score in data["scores"]:
print(score) # 输出每个分数
3. 安全访问(避免KeyError)
使用.get()方法防止键不存在的错误:
email = data.get("user", {}).get("contacts", {}).get("email", "N/A")
print(email) # 若路径存在则返回值,否则返回"N/A"
六、JSON数据的修改与删除
1. 修改数据
data["age"] = 31 # 修改现有键的值
data["new_key"] = "new_value" # 添加新键值对
2. 删除数据
del data["age"] # 删除键"age"
popped_value = data.pop("hobbies", None) # 弹出键"hobbies",返回默认值None
3. 保存修改后的数据
修改后需要重新写入文件:
with open("updated.json", "w") as f:
json.dump(data, f, indent=2)
七、错误处理
1. 解析异常处理
try:
invalid_json = "{name: 'Eve'}" # 错误:键未用双引号包裹
data = json.loads(invalid_json)
except json.JSONDecodeError as e:
print(f"解析失败: {e}")
2. 文件操作异常
try:
with open("nonexistent.json", "r") as f:
data = json.load(f)
except FileNotFoundError:
print("文件不存在!")
八、进阶技巧
1. 处理复杂嵌套结构
使用递归遍历或第三方库(如jsonpath-ng):
from jsonpath_ng import jsonpath, parse
# 查询所有邮箱地址
expression = parse('$.users[*].contacts.email')
for match in expression.find(data):
print(match.value)
2. 自定义序列化
对于复杂对象(如自定义类),需定义default函数:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def encode_person(obj):
if isinstance(obj, Person):
return {"name": obj.name, "age": obj.age}
raise TypeError("类型未支持")
p = Person("Frank", 35)
json_str = json.dumps(p, default=encode_person)
九、总结
JSON因其简洁性和跨平台兼容性成为数据交换的首选格式。通过Python的json模块,可以轻松实现JSON的读写、查询和修改。对于复杂场景,可结合第三方库(如pandas或jsonpath-ng)进一步扩展功能。掌握JSON操作是开发中处理API数据、配置文件和数据持久化的重要技能。
附:常用代码模板
# 读取JSON文件
def read_json(file_path):
with open(file_path, 'r') as f:
return json.load(f)
# 写入JSON文件
def write_json(data, file_path):
with open(file_path, 'w') as f:
json.dump(data, f, indent=2, ensure_ascii=False)