源代碼下載:?learnyaml-cn.yaml
YAML是一個數(shù)據(jù)序列化語言,被設計成人類直接可寫可讀的。
它是JSON的嚴格超集,增加了語法顯著換行符和縮進,就像Python。但和Python不一樣, YAML根本不容許文字制表符。
# YAML中的注解看起來像這樣。
################
# 標量類型 #
################
# 我們的根對象 (它們在整個文件里延續(xù)) 將會是一個地圖,
# 它等價于在別的語言里的一個字典,哈西表或?qū)ο蟆?key: value
another_key: Another value goes here.
a_number_value: 100
scientific_notation: 1e+12
boolean: true
null_value: null
key with spaces: value
# 注意到字符串不需要被引用。但是,它們可以被引用。
"Keys can be quoted too.": "Useful if you want to put a ':' in your key."
# 多行字符串既可以寫成像一個'文字塊'(使用 |),
# 或像一個'折疊塊'(使用 '>')。
literal_block: |
This entire block of text will be the value of the 'literal_block' key,
with line breaks being preserved.
The literal continues until de-dented, and the leading indentation is
stripped.
Any lines that are 'more-indented' keep the rest of their indentation -
these lines will be indented by 4 spaces.
folded_style: >
This entire block of text will be the value of 'folded_style', but this
time, all newlines will be replaced with a single space.
Blank lines, like above, are converted to a newline character.
'More-indented' lines keep their newlines, too -
this text will appear over two lines.
####################
# 集合類型 #
####################
# 嵌套是通過縮進完成的。
a_nested_map:
key: value
another_key: Another Value
another_nested_map:
hello: hello
# 地圖不用有字符串鍵值。
0.25: a float key
# 鍵值也可以是多行對象,用?表明鍵值的開始。
? |
This is a key
that has multiple lines
: and this is its value
# YAML也容許鍵值是集合類型,但是很多語言將會抱怨。
# 序列 (等價于表或數(shù)組) 看起來像這樣:
a_sequence:
- Item 1
- Item 2
- 0.5 # 序列可以包含不同類型。
- Item 4
- key: value
another_key: another_value
-
- This is a sequence
- inside another sequence
# 因為YAML是JSON的超集,你也可以寫JSON風格的地圖和序列:
json_map: {"key": "value"}
json_seq: [3, 2, 1, "takeoff"]
#######################
# 其余的YAML特點 #
#######################
# YAML還有一個方便的特點叫'錨',它讓你簡單地在整個文件里重復內(nèi)容。
# 兩個鍵值將會有相同的值:
anchored_content: &anchor_name This string will appear as the value of two keys.
other_anchor: *anchor_name
# YAML還有標簽,你可以用它顯示地聲明類型。
explicit_string: !!str 0.5
# 一些解析器實現(xiàn)特定語言的標簽,就像這個為了Python的復數(shù)類型。
python_complex_number: !!python/complex 1+2j
####################
# 其余的YAML類型 #
####################
# 字符串和數(shù)字不是僅有的YAML可以理解的標量。
# ISO 格式的日期和日期時間文字也是可以被解析的。
datetime: 2001-12-15T02:59:43.1Z
datetime_with_spaces: 2001-12-14 21:59:43.10 -5
date: 2002-12-14
# 這個!!binary標簽表明一個字符串實際上是一個二進制blob的base64編碼表示。
gif_file: !!binary |
R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
+f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=
# YAML還有一個集合類型,它看起來像這樣:
set:
? item1
? item2
? item3
# 像Python一樣,集合僅是有null數(shù)值的地圖;上面的集合等價于:
set2:
item1: null
item2: null
item3: null
更多建議: