From rookie tutorial, original address: https://www.runoob.com/w3cnote/yaml-intro.html
- It is especially suitable for expressing or editing data structures, various configuration files, dump debugging contents and file outline
- The configuration file suffix of YAML is YML, such as runoob yml
Basic grammar
- Case sensitive (Boolean values are case sensitive)
- Use indents to identify hierarchical relationships
- tap is not allowed for indentation, only spaces are allowed
- The number of indented spaces is not important, as long as the elements of the same level are aligned to the left
- #Identification notes
data type
📖 Object: a collection of key value pairs, also known as mapping / hashes / dictionary
Object key value pairs are represented by a colon structure, and a space should be added after the colon.
You can also use key:{key1: value1, key2: value2,...}.
You can also use indentation to represent hierarchical relationships;
key: child-key: value child-key2: value2
For more complex object formats, you can use a question mark plus a space to represent a complex key, and a colon plus a space to represent a value:
? - complexkey1 - complexkey2 : - complexvalue1 - complexvalue2
That is, the attribute of the object is an array [complexkey1,complexkey2], and the corresponding value is also an array [complexvalue1,complexvalue2]
📖 Array: a set of values arranged in order, also known as sequence / list
Lines beginning with - form an array:
- A - B - C
YAML supports multi-dimensional arrays and can use inline representation:
key: [value1, value2, ...]
If the child member of the data structure is an array, you can indent a space under the item.
- - A - B - C
A relatively complex example:
companies: - id: 1 name: company1 price: 200W - id: 2 name: company2 price: 500W
It means that the companies attribute is an array, and each array element is composed of three attributes: id, name and price.
Arrays can also be expressed in flow:
companies: [{id: 1,name: company1,price: 200W},{id: 2,name: company2,price: 500W}]
📖 Composite structure
Arrays and objects can form a composite structure, for example:
languages: - Ruby - Perl - Python websites: YAML: yaml.org Ruby: ruby-lang.org Python: python.org Perl: use.perl.org
Convert to json:
{ languages: [ 'Ruby', 'Perl', 'Python'], websites: { YAML: 'yaml.org', Ruby: 'ruby-lang.org', Python: 'python.org', Perl: 'use.perl.org' } }
📖 scalars: single, non separable values
Scalar is the most basic and non separable value, including:
- character string
- Boolean value
- integer
- Floating point number
- Null
- time
- date
Use an example to quickly understand the basic use of scalars:
boolean: - TRUE #true,True is OK - FALSE #False, false is OK float: - 3.14 - 6.8523015e+5 #Scientific counting can be used int: - 123 - 0b1010_0111_0100_1010_1110 #Binary representation null: nodeName: 'node' parent: ~ #Use ~ for null string: - ha-ha - 'Hello world' #You can wrap special characters in double quotation marks or single quotation marks - newline newline2 #The string can be split into multiple lines, and each line will be converted into a space date: - 2018-02-17 #The date must be in ISO 8601 format, i.e. yyyy mm DD datetime: - 2018-02-17T15:02:31+08:00 #The time is in ISO 8601 format, the T connection is used between the time and date, and the last + is used to represent the time zone
📖 quote
&Anchor point and * alias, which can be used to refer to:
defaults: &defaults adapter: postgres host: localhost development: database: myapp_development <<: *defaults test: database: myapp_test <<: *defaults
amount to:
defaults: adapter: postgres host: localhost development: database: myapp_development adapter: postgres host: localhost test: database: myapp_test adapter: postgres host: localhost
&It is used to establish anchors, < < means merging into the current data, * is used to reference anchors.
Here is another example:
- &showell Steve - Clark - Brian - Oren - *showell
The code to JavaScript is as follows:
[ 'Steve', 'Clark', 'Brian', 'Oren', 'Steve' ]