JSON 示例集合

探索各种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
      }
    }
  }
}

API响应

高级

典型的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 使用技巧

数据类型

JSON支持字符串、数字、布尔值、数组、对象和null六种数据类型

键名规范

JSON键名必须使用双引号,不能使用单引号或无引号

嵌套结构

合理使用嵌套对象和数组来表示复杂的数据关系

数组使用

数组用于存储有序的数据集合,可以包含任意类型的元素

日期处理

JSON没有日期类型,通常使用ISO 8601格式的字符串

验证工具

使用JSON Schema来验证JSON数据的结构和类型