ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
YAML 的设计目标就是方便人们读写,实质上是通用的数据串行化格式。 [在线验证][1],[阮一峰 YAML教程][2] ## 基本规则 * 大小写敏感 * 使用缩进表示层级关系,缩进只能使用空格 * 文档以“---”作为开头,以“...”作为结尾 * \# 表示注释 * YAML的字符串即使含有空格也不需要引号引起来 ## 纯量 ### 布尔型 `True`和`False` ### 数值 ~~~ number: 1 float: 3.41 ~~~ ### 字符串 ~~~ str: 这是一行字符串 str: !!str 123 # 强制类型转换 ~~~ ### 多行字符串 ~~~ str: 这是 多行 字符串 ~~~ > 多行字符串可以使用`|`保留换行符,也可以使用`>`折叠换行。 > `+`表示保留文字块末尾的换行,`-`表示删除字符串末尾的换行。 ~~~ this: | Foo Bar that: > Foo Bar ~~~ 转为 JavaScript 代码如下。 ~~~ { this: 'Foo\nBar\n', that: 'Foo Bar\n' } ~~~ ~~~ s1: > hi abc s2: >- hi abc s3: >+ Foo hi s4: | Foo hi s5: |- Foo s6: |+ Foo ~~~ 转为 JavaScript 代码如下。 ~~~ { s1: 'hi abc\n', s2: 'hi abc', s3: 'Foo hi\n\n', s4: 'Foo\nhi\n', s5: 'Foo', s6: 'Foo\n' } ~~~ ## 变量 ### 列表 列表使用`'-'`作为分隔符, ~~~ - My Fair Lady - The old boy - The sea ~~~ YAML还有一种内联格式的列表 ~~~ [My Fair Lady, The old boy, The sea] ~~~ ### 字典 ~~~ hello: world hi: yang ~~~ YAML还有一种内联格式的字典 ~~~ {hello: world, hi: yang} ~~~ ## 引用 > 锚点&和别名*,可以用来引用。 ~~~ defaults: &defaults adapter: postgres host: localhost development: database: myapp_development <<: *defaults test: database: myapp_test <<: *defaults ~~~ 等同于下面的代码。 ~~~ defaults: adapter: postgres host: localhost development: database: myapp_development adapter: postgres host: localhost test: database: myapp_test adapter: postgres host: localhost ~~~ &用来建立锚点(defaults),<<表示合并到当前数据,*用来引用锚点。 下面是另一个例子。 ~~~ - &showell Steve - Clark - Brian - Oren - *showell ~~~ 转为 JavaScript 代码如下。 ~~~ [ 'Steve', 'Clark', 'Brian', 'Oren', 'Steve' ] ~~~ [1]:http://nodeca.github.io/js-yaml/ [2]:http://www.ruanyifeng.com/blog/2016/07/yaml.html