1. 程式人生 > 其它 >YAML格式的語法

YAML格式的語法

原文:https://www.cnblogs.com/milton/p/9924179.html

----------------------------------------

YAML格式的語法

基本格式

  • 用空格縮排, 不能用tab
  • 用#標記註釋
  • 列表: 用短劃(-)標記元素
  • 對映: 用冒號(:)分隔key, value. 如果寫在一行, 需要用逗號分隔並前後加花括號
  • 字串: 不加引號, 加單引號或者加雙引號都可以, 加雙引號時可以使用\開頭的轉義字元
  • 多行字串可以用 | 或 > 符號, 緊接著換行符
  • 重複的節點, 可以用 & 標識, 並用 * 來引用

基本元素

列表

# 普通
 - Casablanca
 - North by Northwest
 - The Man Who Wasn't There

# 單行
[milk, pumpkin pie, eggs, juice]
men: [John Smith, Bill Jones]
women:
  - Mary Smith
  - Susan Williams

對映

# Indented Block
   name: John Smith
   age: 33
# Inline Block
 {name: John Smith, age: 33}
- {name: John Smith, age: 33}
- name: Mary Smith
  age: 27

多行字串

data: |
   There once was a short man from Ealing
   Who got on a bus to Darjeeling
       It said on the door
       "Please don't spit on the floor"
   So he carefully spat on the ceiling

data: >
   Wrapped text
   will be folded
   into a single
   paragraph

   Blank lines denote
   paragraph breaks

指定型別

a: 123                     # an integer
b: "123"                   # a string, disambiguated by quotes
c: 123.0                   # a float
d: !!float 123             # also a float via explicit data type prefixed by (!!)
e: !!str 123               # a string, disambiguated by explicit type
f: !!str Yes               # a string via explicit type
g: Yes                     # a boolean True (yaml1.1), string "Yes" (yaml1.2)
h: Yes we have No bananas  # a string, "Yes" and "No" disambiguated by context.

picture: !!binary |
  R0lGODdhDQAIAIAAAAAAANn
  Z2SwAAAAADQAIAAACF4SDGQ
  ar3xxbJ9p0qa7R0YxwzaFME
  1IAADs=
myObject:  !myClass { name: Joe, age: 15 }