yaml和json互转测试网站1
yaml和json互转测试网站2
支持的数据类型
YAML 对象
对象键值对使用冒号结构表示 key: value,冒号后面要加一个空格。
也可以使用 key:{key1: value1, key2: value2, ...}。
还可以使用缩进表示层级关系;
较为复杂的对象格式,可以使用问号加一个空格代表一个复杂的 key,配合一个冒号加一个空格代表一个 value:
- complexkey1 - complexkey2 - complexvalue1 - complexvalue2 # 意思即对象的属性是一个数组 [complexkey1,complexkey2],对应的值也是一个数组 [complexvalue1,complexvalue2] 'complexkey1,complexkey2': [ 'complexvalue1', 'complexvalue2' ]YAML 数组
以 - 开头的行表示构成一个数组,如果不加-则会被合并成一个元素,比如:
- val1 - val2 - val3 # YAML 支持多维数组,可以使用行内表示,所以以上数组等价于: key: [val1, val2, val3] # 如果不加 - # 则上述内容会被表述为: key: 'val1 val2 val3'数据结构的子成员是一个数组,则可以在该项下面缩进一个空格。
#转为 JavaScript 如下。 [ [ 'Cat', 'Dog', 'Goldfish' ] ]一个相对复杂的例子:
companies:
id: 1
name: company1
price: 200W
id: 2
name: company2
price: 500W
# 意思是 companies 属性是一个数组,每一个数组元素又是由 id、name、price 三个属性构成。
# 数组也可以使用流式(flow)的方式表示:
companies: [{id: 1,name: company1,price: 200W},{id: 2,name: company2,price: 500W}]
数组和对象可以构成复合结构,例:
languages:
- Ruby
- Perl
- Python
websites:
YAML: yaml.org
Ruby: ruby-lang.org
Python: python.org
Perl: use.perl.org
# 转换为 json 为:
languages: [ 'Ruby', 'Perl', 'Python'],
websites: {
YAML: 'yaml.org',
Ruby: 'ruby-lang.org',
Python: 'python.org',
Perl: 'use.perl.org'
纯量是最基本的,不可再分的值,包括:
Null(用~表示,即ESC键下面的那个)
YAML 允许使用两个感叹号,强制转换数据类型。
e: !!str 123
f: !!str true
# 转为 JavaScript 如下。
{ e: '123', f: 'true' }
字符串是最常见,也是最复杂的一种数据类型。字符串默认不使用引号表示。
str: 这是一行字符串
# 转为 JavaScript 如下。
{ str: '这是一行字符串' }
# 如果字符串之中包含空格或特殊字符,需要放在引号之中。
str: '内容: 字符串'
# 转为 JavaScript 如下。
{ str: '内容: 字符串' }
# 单引号和双引号都可以使用,单引号会主动对特殊字符转义,双引号不会主动对特殊字符转义。
s1: '内容\n字符串'
s2: "内容\n字符串"
# 转为 JavaScript 如下。
{ s1: '内容\\n字符串', s2: '内容\n字符串' }
# 单引号之中如果还有单引号,必须连续使用两个单引号转义。
str: 'labor''s day'
# 转为 JavaScript 如下。
{ str: 'labor\'s day' }
# 字符串可以写成多行,从第二行开始,必须有一个单空格缩进。换行符会被转为空格。
str: 这是一段
# 转为 JavaScript 如下。
{ str: '这是一段 多行 字符串' }
# 多行字符串可以使用|保留换行符,也可以使用>折叠换行。
this: |
that: >
end:~
# 转为 JavaScript 代码如下。
{ this: 'Foo\nBar\n', that: 'Foo Bar\n' }
# +表示保留文字块末尾的换行,-表示删除字符串末尾的换行。
s1: |
s2: |+
s3: |-
s4: |-
s5: >-
# 转为 JavaScript 代码如下。
{ s1: 'Foo\n', s2: 'Foo\n\n\n', s3: 'Foo' , s4: 'Foo\nBar', s5: 'Foo'}
# 字符串之中可以插入 HTML 标记。
message: |
<p style="color: red">
end:~
# 转为 JavaScript 如下。
{ message: '\n<p style="color: red">\n 段落\n</p>\n' }
使用一个例子来快速了解纯量的基本使用:
boolean:
- TRUE #true,True都可以
- FALSE #false,False都可以
float:
- 3.14
- 6.8523015e+5 #可以使用科学计数法
- 123
- 0b1010_0111_0100_1010_1110 #二进制表示
null:
nodeName: 'node'
parent: ~ #使用~表示null
string:
- 'Hello world' #可以使用双引号或者单引号包裹特殊字符
- newline
newline2 #字符串可以拆成多行,每一行会被转化成一个空格
date:
- 2018-02-17 #日期必须使用ISO 8601格式,即yyyy-MM-dd
datetime:
- 2018-02-17T15:02:31+08:00 #时间使用ISO 8601格式,时间和日期之间使用T连接,最后使用+代表时区
& 锚点和 * 别名,可以用来引用:
defaults: &defaults
adapter: postgres
host: localhost
development:
database: myapp_development
<<: *defaults
test:
database: myapp_test
<<: *defaults
# 相当于:
defaults:
adapter: postgres
host: localhost
development:
database: myapp_development
adapter: postgres
host: localhost
test:
database: myapp_test
adapter: postgres
host: localhost
& 用来建立锚点(defaults),<< 表示合并到当前数据,* 用来引用锚点。
下面是另一个例子:
- &showell Steve
- Clark
- Brian
- Oren
- *showell
# 转为 JavaScript 代码如下:
[ 'Steve', 'Clark', 'Brian', 'Oren', 'Steve' ]
# Collection Types #############################################################
################################################################################
# http://yaml.org/type/map.html -----------------------------------------------#
# Unordered set of key: value pairs.
Block style: !!map
Clark : Evans
Ingy : döt Net
Oren : Ben-Kiki
Flow style: !!map { Clark: Evans, Ingy: döt Net, Oren: Ben-Kiki }
# http://yaml.org/type/omap.html ----------------------------------------------#
omap:
# Explicitly typed ordered map (dictionary).
Bestiary: !!omap
- aardvark: African pig-like ant eater. Ugly.
- anteater: South-American ant eater. Two species.
- anaconda: South-American constrictor snake. Scaly.
# Etc.
# Flow style
Numbers: !!omap [ one: 1, two: 2, three : 3 ]
# http://yaml.org/type/pairs.html ---------------------------------------------#
pairs:
# Explicitly typed pairs.
Block tasks: !!pairs
- meeting: with team.
- meeting: with boss.
- break: lunch.
- meeting: with client.
Flow tasks: !!pairs [ meeting: with team, meeting: with boss ]
# http://yaml.org/type/set.html -----------------------------------------------#
# Explicitly typed set.
baseball players: !!set
? Mark McGwire
? Sammy Sosa
? Ken Griffey
# Flow style
baseball teams: !!set { Boston Red Sox, Detroit Tigers, New York Yankees }
# http://yaml.org/type/seq.html -----------------------------------------------#
# Ordered sequence of nodes
Block style: !!seq
- Mercury # Rotates - no light/dark sides.
- Venus # Deadliest. Aptly named.
- Earth # Mostly dirt.
- Mars # Seems empty.
- Jupiter # The king.
- Saturn # Pretty.
- Uranus # Where the sun hardly shines.
- Neptune # Boring. No rings.
- Pluto # You call this a planet?
Flow style: !!seq [ Mercury, Venus, Earth, Mars, # Rocks
Jupiter, Saturn, Uranus, Neptune, # Gas
Pluto ] # Overrated
# Scalar Types #################################################################
################################################################################
# http://yaml.org/type/bool.html ----------------------------------------------#
bool:
- true
- True
- TRUE
- false
- False
- FALSE
# http://yaml.org/type/float.html ---------------------------------------------#
float:
canonical: 6.8523015e+5
exponentioal: 685.230_15e+03
fixed: 685_230.15
negative infinity: -.inf
not a number: .NaN
# http://yaml.org/type/int.html -----------------------------------------------#
canonical: 685230
decimal: +685_230
octal: 0o2472256
hexadecimal: 0x_0A_74_AE
binary: 0b1010_0111_0100_1010_1110
# http://yaml.org/type/merge.html ---------------------------------------------#
merge:
- &CENTER { x: 1, y: 2 }
- &LEFT { x: 0, y: 2 }
- &BIG { r: 10 }
- &SMALL { r: 1 }
# All the following maps are equal:
- # Explicit keys
r: 10
label: nothing
- # Merge one map
<< : *CENTER
r: 10
label: center
- # Merge multiple maps
<< : [ *CENTER, *BIG ]
label: center/big
- # Override
<< : [ *BIG, *LEFT, *SMALL ]
label: big/left/small
# http://yaml.org/type/null.html ----------------------------------------------#
null:
# This mapping has four keys,
# one has a value.
empty:
canonical: ~
english: null
~: null key
# This sequence has five
# entries, two have values.
sparse:
- 2nd entry
- 4th entry
- Null
# http://yaml.org/type/str.html -----------------------------------------------#
string: abcd
# http://yaml.org/type/timestamp.html -----------------------------------------#
timestamp:
canonical: 2001-12-15T02:59:43.1Z
valid iso8601: 2001-12-14t21:59:43.10-05:00
space separated: 2001-12-14 21:59:43.10 -5
no time zone (Z): 2001-12-15 2:59:43.10
date (00:00:00Z): 2002-12-14
# Custom types #################################################################
################################################################################
# JS-YAML allows you to specify a custom YAML types for your structures.
# This is a simple example of custom constructor defined in `js/demo.js` for
# custom `!sexy` type:
# var SexyYamlType = new jsyaml.Type('!sexy', {
# kind: 'sequence',
# construct: function (data) {
# return data.map(function (string) { return 'sexy ' + string; });
# }
# });
# var SEXY_SCHEMA = jsyaml.Schema.create([ SexyYamlType ]);
# result = jsyaml.load(yourData, { schema: SEXY_SCHEMA });