YAML 基础讲解
YAML 转 json 网站: https://nodeca.github.io/js-yaml/
YAML 基本语法
-
对大小写敏感
-
用缩进表示层级关系
-
不能使用 tab 键,使用空格缩进
-
空格数只需同层级元素保持一致即可
-
用#注释
YAML 语法+实例
键值对
-
键值对用
key: value
表示,冒号后面需要加一个空格; -
也可以如下表示
user: tony
对应json:
{ user: 'tony' }
数组
数组以-加一个空格开头
- A
- B
- C
对应的json:
[ 'A', 'B', 'C' ]
复合实例讲解
%yaml 1.2 # 参考版本
---
shudent:
user: tony
sex: man
score:
- math: !!str 100 # !!str 表示判断为字符串类型
- english: !!float 100.0 # !!float 表示判断为浮点数类型
- Chinese: 100
fly: true # 布尔类型,也可以是false
file: null # null 表示为空
file0:
birthday: 2021-01-01 10:00:00 # 时间格式为年月日用杠连接,时每秒用冒号连接
pi: 314e-2 # 科学计数法,相当于314*10^-2
book:
abc
def
ghi
book1: >
abc
def
ghi
book2: |
abc
def
ghi
test: &i # 相当于定义指针
abc: 1
bcd: 2
test1:
*i # 相当于指针的引用
--- # 创建另外一个文件
... # 表示文件结束
对应json:
{ shudent:
{ user: 'tony',
sex: 'man',
score: [ { math: '100' }, { english: 100 }, { Chinese: 100 } ],
fly: true,
file: null,
file0: null,
birthday: Fri Jan 01 2021 18:00:00 GMT+0800 (中国标准时间),
pi: 3.14,
book: 'abc def ghi',
book1: 'abc def ghi\n',
book2: 'abc\ndef\nghi\n',
test: { abc: 1, bcd: 2 },
test1: { abc: 1, bcd: 2 } } }