原文: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 }