JSON 示例

常用的 JSON 数据结构示例,一键复制使用

什么是 JSON?

JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。它基于 JavaScript 的对象字面量语法,但是独立于语言,几乎所有现代编程语言都支持 JSON 的解析和生成。

JSON 具有以下特点:

  • 易于人阅读和编写
  • 易于机器解析和生成
  • 基于文本,完全独立于语言
  • 广泛用于 Web 应用程序中的数据传输

简单对象

基本的 JSON 对象结构

{
  "name": "John Doe",
  "age": 30,
  "city": "New York"
}

用户资料

包含嵌套对象的用户信息

{
  "id": 1,
  "name": "Alice Smith",
  "email": "alice@example.com",
  "profile": {
    "avatar": "https://example.com/avatar.jpg",
    "bio": "Software Developer",
    "location": "San Francisco"
  },
  "preferences": {
    "theme": "dark",
    "notifications": true
  }
}

产品列表

JSON 数组包含多个产品对象

[
  {
    "id": "p001",
    "name": "Laptop",
    "price": 999.99,
    "category": "Electronics",
    "inStock": true
  },
  {
    "id": "p002",
    "name": "Mouse",
    "price": 29.99,
    "category": "Accessories",
    "inStock": false
  }
]

API 响应

典型的 REST API 响应格式

{
  "status": "success",
  "code": 200,
  "message": "Data retrieved successfully",
  "data": {
    "users": [
      {
        "id": 1,
        "name": "John",
        "role": "admin"
      },
      {
        "id": 2,
        "name": "Jane",
        "role": "user"
      }
    ],
    "pagination": {
      "page": 1,
      "limit": 10,
      "total": 25
    }
  },
  "timestamp": "2024-01-01T12:00:00Z"
}

配置文件

应用程序配置 JSON

{
  "app": {
    "name": "MyApp",
    "version": "1.2.0",
    "environment": "production"
  },
  "database": {
    "host": "localhost",
    "port": 5432,
    "name": "myapp_db",
    "ssl": true
  },
  "features": {
    "authentication": true,
    "logging": true,
    "analytics": false
  }
}

嵌套数组

包含多层嵌套的复杂结构

{
  "company": "Tech Corp",
  "departments": [
    {
      "name": "Engineering",
      "teams": [
        {
          "name": "Frontend",
          "members": [
            "Alice",
            "Bob",
            "Charlie"
          ]
        },
        {
          "name": "Backend",
          "members": [
            "David",
            "Eve",
            "Frank"
          ]
        }
      ]
    },
    {
      "name": "Marketing",
      "teams": [
        {
          "name": "Digital",
          "members": [
            "Grace",
            "Henry"
          ]
        }
      ]
    }
  ]
}

错误响应

API 错误响应示例

{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Invalid input parameters",
    "details": [
      {
        "field": "email",
        "message": "Email format is invalid"
      },
      {
        "field": "password",
        "message": "Password must be at least 8 characters"
      }
    ]
  },
  "timestamp": "2024-01-01T12:00:00Z",
  "requestId": "req_123456789"
}

GeoJSON

地理位置数据格式

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -74.006,
          40.7128
        ]
      },
      "properties": {
        "name": "New York City",
        "population": 8336817
      }
    }
  ]
}

Package.json

Node.js 项目配置文件

{
  "name": "my-project",
  "version": "1.0.0",
  "description": "A sample Node.js project",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "jest",
    "build": "webpack --mode production"
  },
  "dependencies": {
    "express": "^4.18.0",
    "lodash": "^4.17.21"
  },
  "devDependencies": {
    "jest": "^29.0.0",
    "webpack": "^5.74.0"
  }
}

混合数据类型

包含各种 JSON 数据类型的示例

{
  "string": "Hello World",
  "number": 42,
  "float": 3.14159,
  "boolean": true,
  "nullValue": null,
  "array": [
    1,
    "two",
    true,
    null
  ],
  "object": {
    "nested": "value"
  },
  "emptyArray": [],
  "emptyObject": {}
}

功能特性

丰富示例

提供 10 种常用的 JSON 数据结构示例

一键复制

点击即可复制 JSON 代码到剪贴板

语法说明

详细的 JSON 语法规则和数据类型说明

隐私安全

所有操作在本地进行,不上传任何数据

常见问题

是的,所有示例都是有效的 JSON 格式,可以直接复制使用。你可以根据需要修改其中的数据。
JSON 语法简单易学。主要包括对象({})、数组('[]')、字符串、数字、布尔值和 null。字符串必须用双引号包围,键值对用冒号分隔。
JSON 是数据格式,JavaScript 对象是编程语言的数据结构。JSON 要求键必须是字符串且用双引号包围,而 JavaScript 对象的键可以不用引号。
你可以使用我们的 JSON 验证器工具来检查 JSON 格式是否正确。它会指出语法错误的具体位置。
JSON 支持六种基本数据类型:对象(使用花括号)、数组(使用方括号)、字符串(必须用双引号)、数字、布尔值(true/false)和 null。
这是 JSON 规范的要求。与 JavaScript 对象不同,JSON 是严格的数据格式,所有的键都必须是用双引号包围的字符串,这确保了跨语言的兼容性。