欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

YAML格式官网示例 - : # [ ] { } --- ... & * ? > | " " ' ' !! !

程序员文章站 2022-03-02 14:17:25
...

其实对于yaml格式的理解应该结合数组来理解


1.' - '队列

- list 1
- list 2
- list 3


对应于数组当中就是 array[3] 其中


array[0]="list 1"
array[1]="list 2"
array[2]="list 3"


一个'-'就代表当前数组结构新增加一行


2.' : 'map

- key1 : value1
- key2 : value2
- key3 : value3


key只是获取到value的名称,就像是用变量名获取到数据一样(a=30)这个时候使用a就能够得到30这个值

array[0]=value1    equal array["key1"]=value1
array[1]=value2       equal array["key2"]=value2
array[2]=value3    equal array["key3"]=value3


':'和'-'可以任意组合  array[2][3]


american:              array["american"].size=3
  - Boston Red Sox     array["american"][0]="Boston Red Sox"
  - Detroit Tigers     array["american"][1]="Detroit Tigers "
  - New York Yankees   array["american"][2]="New York Yankees"
national:           array["national"].size=3
  - New York Mets      array["national"][0]="New York Mets"
  - Chicago Cubs       array["national"][0]="Chicago Cubs"
  - Atlanta Braves     array["national"][0]="Atlanta Braves"

array[2][3]
-
  name: Mark McGwire    array[0]["name"]="Mark McGwire"
  hr:   65        array[0]["hr"]=65
  avg:  0.278        array[0]["avg"]=0.278
-
  name: Sammy Sosa    array[0]["name"]="Sammy Sosa"
  hr:   63        array[0]["hr"]=63
  avg:  0.288        array[0]["avg"]=0.288

3.' # '注释


hr:  65    # Home runs
array["hr"]=65

4. " [ ] " 队列 直接用逗号来断的队列
                array[3][3]

- [name,hr,avg]            array[0][0]=name      array[0][1]=hr array[0][2]=avg
- [Mark McGwire,65,0.278]    array[1][0]=Mark McGwire array[1][1]=65 array[1][2]=0.278
- [Sammy Sosa,63,0.288]        array[2][0]=Mark McGwire array[2][1]=63 array[2][2]=0.288

Mark McGwire: [name,hr,avg]    array["Mark McGwire"][0]=name array["Mark McGwire"][1]=hr array["Mark McGwire"][2]=avg

5. " {} "集合 直接用逗号来断 可以里面在放map array[2][2]
 

Mark McGwire: {hr: 65, avg: 0.278}    array["Mark McGwire"]["hr"]=65 array["Mark McGwire"]["avg"]=0.278
Sammy Sosa: {hr: 63,avg: 0.288}        array["Sammy Sosa"]["hr"]=63 array["Sammy Sosa"]["avg"]=0.288

6." --- "新文档的标识 一次解析结束,yaml意义上的不同文件,只是本地文件上是在同一份文件里面

# 文件1
---
- Mark McGwire
- Sammy Sosa
- Ken Griffey

# 文件2
---
- Chicago Cubs
- St Louis Cardinals

7." ... "文档结束标示
 

time: 20:03:20
player: Sammy Sosa
action: strike (miss)
...

8." & " " * "锚点 &标记锚点 *取锚点指向内容 类似于指针
 

hr:                hr:
  - Mark McGwire          - Mark McGwire
  - &SS Sammy Sosa          - Sammy Sosa        &ss指向内容Sammy Sosa
rbi:                rbi:
  - *SS                  - Sammy Sosa        *SS 取出内容
  - Ken Griffey              - Ken Griffey

9." ?  "question mark and space 复杂map 多个key对应一个value键是一个列表或键值表
 

? - Detroit Tigers    array["Detroit Tigers"]=2001-07-23
  - Chicago cubs    array["Chicago cubs"]=2001-07-23
:
  - 2001-07-23
? [ New York Yankees,        array["New York Yankees"]=[ 2001-07-02, 2001-08-12,2001-08-14 ]
    Atlanta Braves ]        array[" Atlanta Braves"]=[ 2001-07-02, 2001-08-12,2001-08-14 ]
: [ 2001-07-02, 2001-08-12,     也就是说 array["New York Yankees"][0]=2001-07-02
    2001-08-14 ]                 array["New York Yankees"][1]=2001-08-12
- item    : Super Hoop        array[0]["item"]=Super Hoop
  quantity: 1            array[0]["quantity"]=1    
- item    : Basketball        array[1]["item"]=Basketball
  quantity: 4            array[1]["quantity"]=4
- item    : Big Shoes        array[2]["item "]=Big Shoes
  quantity: 1            array[1]["quantity"]=1


10." | "所有的换行符会被保留
 

--- |
  \//||\/||
  // ||  ||__

11." > "折叠标志 新的一行会被空格代替  结束折叠标志是遇到空行或者是更进一步缩进

--- >
  Mark McGwire's        Mark McGwire's year was crippled by a knee injury.
  year was crippled
  by a knee injury.

 

>
 Sammy Sosa completed another        Sammy Sosa completed another fine season with great stats.
 fine season with great stats.                       63 Home Runs
                                                                          0.288 Batting Average
        63 Home Runs                                  What a year!
        0.288 Batting Average

 What a year!
name: Mark McGwire               array["name"]=Mark McGwire
accomplishment: >                   array["accomplishment"]=Mark set a major league home run record in 1998.
  Mark set a major league
  home run record in 1998.
stats: |                                         array["stats"]=65 Home Runs
  65 Home Runs                       0.278 Batting Average
  0.278 Batting Average

12." "" "和" '' "区别
""支持转义字符串
''不支持 支持转义字符串
13." !! "声明数据类型 " ! "声明自定义数据类型
 

not-date: !!str 2002-04-28   #声明2002-04-28是字符串类型不是日期类型

picture: !!binary |                  #声明2002-04-28是字符串类型不是日期类型
 R0lGODlhDAAMAIQAAP//9/X
 17unp5WZmZgAAAOfn515eXv
 Pz7Y6OjuDg4J+fn5OTk6enp
 56enmleECcgggoBADs=
--- !!set             set自动给map分配null value
? Mark McGwire
? Sammy Sosa
? Ken Griff
--- !!omap            omap声明每个map只有一个key
- Mark McGwire: 65
- Sammy Sosa: 63
- Ken Griffy: 58