什么是 YAML
YAML 是一個數據序列化語言,方便人類閱讀,可用于所有編程語言。
YAML is a human friendly data serialization standard for all programming languages.
應用場景
可以用于 conf 配置
我用 YAML 是配置一個服務器的頁面路由
。
YAML 長什么樣子
# 記錄一個人的體檢信息
# (鍵值對)
age: 65 # 年齡
height: 178 # 身高
weight: 147 # 體重
# 記錄出門要做的事情
# (鍵值對 + 數組列表)
去菜市場買:
- 豆腐
- 蘿卜
去商店買:
- 砧板
- 砂鍋
YAML 語法詳解
YAML’s block collections use indentation for scope and begin each entry on its own line. Block sequences indicate each entry with a dash and space ( “- ”). Mappings use a colon and space (“: ”) to mark each key: value pair. Comments begin with an octothorpe (also called a “hash”, “sharp”, “pound”, or “number sign” - “#”).
- YAML 用縮進來表示層級關系,每行是一個語句。
-
塊級列表 使用短橫線和空格開頭,如 "- "
- C
- PHP
- javascript
-
鍵值對 使用分號和空格來隔開鍵和值,如 "key: value"
C: 系統語言
PHP: 服務器語言
javascript: 網頁端語言
```
-
注釋 以井號開頭,如 "# 這是一行注釋"
javascript: 網頁端語言 #使用 nodejs 也可以寫服務器腳本
```
YAML also has flow styles, using explicit indicators rather than indentation to denote scope. The flow sequence is written as a comma separated list within square brackets. In a similar manner, the flow mapping uses curly braces.
- 層級關系 除了使用縮進,還能夠使用特定符號來表示。
- 數組 使用方括號和逗號來表示
- [Sammy Sosa , 63, 0.288]
- [Ken Sosa , 70, 0.302]
- 多個鍵值對 使用花括號的逗號來表示
Mark McGwire: {hr: 65, avg: 0.278}
更多
以上為最常用的 YAML 語法,更復雜的語法請參見 英文官網