YAML入门

时间:2024-10-25 23:34:32

概要

YAML(是YAML Ain't Markup Language的缩写,尾音的发音类似Camel)是一种序列化数据的语言(类似json, xml),使用轻量高可读性的语法描述list, dict等数据结构。

可以把YAML看作一个比json更轻量易读的数据序列化表示语言,该语言使用缩进而不使用括号,在多层嵌套时有更好的可读性,也更有利于grep, awk等进行操作。

语法

略……

示例

相同的数据使用YAML与json表示,对比如下:

1. list

YAML

- Mark McGwire
- Sammy Sosa
- Ken Griffey

JSON

[
"Mark McGwire",
"Sammy Sosa",
"Ken Griffey"
]

2. list type 2

YAML

[Mark McGwire, Tom Curry, Steve Thompson]

JSON

[
"Mark McGwire",
"Tom Curry",
"Steve Thompson"
]

3. dict

YAML

hr:  65
avg: 0.278
rbi: 147

JSON

{
"hr": 65,
"avg": 0.27800000000000002,
"rbi": 147
}

4. dict involve list

YAML

american:
- Boston Red Sox
- Detroit Tigers
- New York Yankees
national:
- New York Mets
- Chicago Cubs
- Atlanta Braves

JSON

{
"american": [
"Boston Red Sox",
"Detroit Tigers",
"New York Yankees"
],
"national": [
"New York Mets",
"Chicago Cubs",
"Atlanta Braves"
]
}

5. dict involve dict

YAML

Mark McGwire: {hr: 65, avg: 0.278}
Sammy Sosa: {
hr: 63,
avg: 0.288
}

JSON

{
"Sammy Sosa": {
"hr": 63,
"avg": 0.28799999999999998
},
"Mark McGwire": {
"hr": 65,
"avg": 0.27800000000000002
}
}

6. list involve list

YAML

- [name        , hr, avg  ]
- [Mark McGwire, 65, 0.278]
- [Sammy Sosa , 63, 0.288]

JSON

[
[
"name",
"hr",
"avg"
],
[
"Mark McGwire",
65,
0.27800000000000002
],
[
"Sammy Sosa",
63,
0.28799999999999998
]
]

Ref

Official Site

http://yaml.org/

Wiki

https://zh.wikipedia.org/zh-cn/YAML

YAML中文快速入门,有思维导图

http://www.cnblogs.com/chwkai/archive/2005/10/07/beginning_yaml.html

Online YAML parser (parse YAML to Json/XML)

https://yaml-online-parser.appspot.com/