JSON Grammar
10 methodsJSON 的数据类型与语法规则,定义值、结构与转义。
{ "key": value, ... }对象:无序键值对集合,键必须是双引号字符串。
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | 双引号包裹的字符串键 |
| value | any | 任意 JSON 值 |
Returns
一个 JSON 对象
Example
json
{
"name": "Alice",
"age": 30,
"active": true
}[ value, value, ... ]数组:有序值集合,值可为任意 JSON 类型。
Returns
一个 JSON 数组
Example
json
[1, 2, 3, 4, 5]
["apple", "banana", "cherry"]
[
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"}
]"text"字符串:双引号包裹的 Unicode 字符序列。
Returns
一个 JSON 字符串
Example
json
"hello world"
"中文测试"
"escaped: \"quoted\""number数字:整数或浮点数,支持负号和科学计数法,不支持 NaN/Infinity。
Returns
一个 JSON 数字
Example
json
42
-17
3.14
1.5e3
-2.5E-4true | false布尔值:小写字面量 true 或 false。
Returns
一个 JSON 布尔值
Example
json
{
"admin": true,
"verified": false
}null空值:小写字面量 null,表示无值。
Returns
一个 JSON null 值
Example
json
{
"nickname": null,
"deletedAt": null
}nested { } and [ ]嵌套:对象和数组可任意层级互相嵌套。
Returns
嵌套的 JSON 结构
Example
json
{
"user": {
"id": 42,
"name": "Alice",
"roles": ["admin", "editor"],
"address": {
"city": "Beijing",
"zip": "100000"
}
}
}\" \\ \/ \b \f \n \r \t \uXXXX转义字符:字符串中用反斜杠转义特殊字符。
Returns
经过转义的合法 JSON 字符串
Example
json
{
"path": "C:\\Users\\name",
"multiLine": "line1\nline2",
"tabbed": "a\tb\tc",
"unicode": "\u4e2d\u6587",
"quoted": "He said \"hi\""
}// 或 /* */ (不支持)JSON 标准不支持注释,添加注释会导致解析失败,需用 JSONC 等扩展格式。
Returns
标准 JSON 中注释非法,解析器报错
Example
json
{
"name": "Alice"
// 这是注释,标准 JSON 解析器会报错
/* 块注释也不行 */
}
// 替代方案:把注释作为字段
{
"_comment": "这是用户配置",
"name": "Alice"
}{ "$schema": ..., "type": ..., "properties": ... }JSON Schema 基础:用 JSON 描述 JSON 数据的结构与约束。
Returns
一个 JSON Schema 文档,可用于校验实例
Example
json
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["name", "age"],
"properties": {
"name": { "type": "string", "minLength": 1 },
"age": { "type": "integer", "minimum": 0 },
"email": { "type": "string", "format": "email" }
}
}