ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 语法规则 * 大小写敏感 * 缩进时不允许使用Tab键,只允许使用空格。 * 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可 * 表示注释,从这个字符一直到行尾,都会被解析器忽略。 ## 支持数据格式 * 对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary) * 数组:一组按次序排列的值,又称为序列(sequence) / 列表(list) * 纯量(scalars):单个的、不可再分的值 ### 对象 `animal: pets` => js `{ animal: 'pets' } ` 采用行内对象 `hash: { name: Steve, foo: bar } ` =>js `{ hash: { name: 'Steve', foo: 'bar' } } ` ### 数组 1. 用连线开头的表示数组 ``` - Cat - Dog - Goldfish ``` =>js `[ 'Cat', 'Dog', 'Goldfish' ] ` 2. 多为数组 ``` - - Cat - Dog - Goldfish ``` => js `[ [ 'Cat', 'Dog', 'Goldfish' ] ] ` 3. 数组也可以采用行内表示法 `animal: [Cat, Dog] ` => js `{ animal: [ 'Cat', 'Dog' ] } ` ### 复合结构 ``` languages: - Ruby - Perl - Python websites: YAML: yaml.org Ruby: ruby-lang.org Python: python.org Perl: use.perl.org ``` => js ``` { languages: [ 'Ruby', 'Perl', 'Python' ], websites: { YAML: 'yaml.org', Ruby: 'ruby-lang.org', Python: 'python.org', Perl: 'use.perl.org' } } ``` ## 纯量 不可再分的元素 * 字符串 * 布尔值 `isSet: true ` -> js `{isSet: true }` * 整数 * 浮点数 `number: 12.30 ` ->js `{number:12.30}` * Null `parent: ~ ` -> `{parent:null}` * 时间 `iso8601: 2001-12-14t21:59:43.10-05:00 ` ->js `{ iso8601: new Date('2001-12-14t21:59:43.10-05:00') } ` * 日期 `date: 1976-07-31 ` -> js `{ date: new Date('1976-07-31') } ` ### 字符串 1. 双引号不会对特殊字符转义 ``` s1: '内容\n字符串' s2: "内容\n字符串" ``` => js `{ s1: '内容\\n字符串', s2: '内容\n字符串' } ` 2. 字符串可以写成多行,从第二行开始,必须有一个单空格缩进。换行符会被转为空格 ``` str: 这是一段 多行 字符串 ``` => js `{ str: '这是一段 多行 字符串' } ` 3. 多行字符串可以使用|保留换行符,也可以使用>折叠换行 ``` this: | Foo Bar that: > Foo Bar ``` => js `{ this: 'Foo\nBar\n', that: 'Foo Bar\n' } ` 4. +表示保留文字块末尾的换行,-表示删除字符串末尾的换行 ``` s1: | Foo s2: |+ Foo s3: |- Fo ``` => js ``` { s1: 'Foo\n', s2: 'Foo\n\n\n', s3: 'Foo' } ``` ### 强制转换数据类型 使用两个 `!!` ``` e: !!str 123 f: !!str true ``` => js `{e:"123",f:"true"}`