探索各种JSON数据结构和使用场景,从基础对象到复杂的API响应,帮助您更好地理解和使用JSON
简单的JSON对象结构,包含基本数据类型
{ "name": "张三", "age": 25, "email": "zhangsan@example.com", "isActive": true, "scores": [85, 92, 78] }
JSON数组的使用,包含多个对象
[ { "id": 1, "name": "产品A", "price": 29.99 }, { "id": 2, "name": "产品B", "price": 39.99 } ]
嵌套的JSON对象,展示层级结构
{ "user": { "name": "李四", "profile": { "age": 30, "address": { "city": "北京", "country": "中国" } } } }
应用程序配置文件,包含多种设置
{ "app": { "name": "MyApp", "version": "1.0.0", "settings": { "theme": "dark", "language": "zh-CN", "notifications": { "email": true, "push": false, "sms": true } } } }
典型的REST API响应数据结构
{ "status": "success", "code": 200, "data": { "users": [ {"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"} ], "pagination": { "page": 1, "total": 100 } } }
错误响应和异常处理的标准格式
{ "error": { "code": "VALIDATION_ERROR", "message": "输入参数验证失败", "details": [ { "field": "email", "message": "邮箱格式不正确" } ], "timestamp": "2024-01-20T10:30:00Z" } }
完整的用户配置和偏好设置
{ "user": { "id": "user123", "profile": { "displayName": "王五", "avatar": "https://example.com/avatar.jpg", "bio": "软件工程师", "location": "上海" }, "preferences": { "theme": "light", "language": "zh-CN", "timezone": "Asia/Shanghai" } } }
电商产品目录数据结构
{ "catalog": { "products": [ { "id": "P001", "name": "iPhone 15 Pro", "price": 999.99, "category": "Smartphones", "inStock": true, "specs": { "display": "6.1\"", "storage": "256GB", "color": "Natural Titanium" } } ] } }
应用程序日志和监控数据
{ "log": { "timestamp": "2024-01-20T15:30:45.123Z", "level": "INFO", "message": "用户登录成功", "context": { "userId": "user123", "ip": "192.168.1.100", "userAgent": "Mozilla/5.0..." }, "metadata": { "requestId": "req_abc123", "sessionId": "sess_xyz789" } } }
JSON支持字符串、数字、布尔值、数组、对象和null六种数据类型
JSON键名必须使用双引号,不能使用单引号或无引号
合理使用嵌套对象和数组来表示复杂的数据关系
数组用于存储有序的数据集合,可以包含任意类型的元素
JSON没有日期类型,通常使用ISO 8601格式的字符串
使用JSON Schema来验证JSON数据的结构和类型