transferred from: Http://www.ruanyifeng.com/blog/2016/07/yaml.html?f=tt
YAML Language Tutorials
Author: Ruan Yi Feng
Date: July 4, 2016
Programming is unavoidable to write configuration files, how to write configuration is also a learning.
YAML is a language dedicated to writing configuration files, very concise and powerful, far more convenient than JSON format.
This article describes the syntax of YAML, taking the implementation of JS-YAML as an example. You can go to the online Demo to verify the example below.
First, Introduction
The goal of the YAML language (pronounced/ˈjæməl/) is to facilitate human literacy. It is essentially a general purpose data serialization format.
It has the following basic syntax rules. Case sensitive use indentation to indicate that a hierarchical relationship is indented without using the TAB key, only spaces are allowed. The number of spaces you indent is not important, as long as the same level elements are left aligned
# indicates a comment, which is ignored by the parser, from the word selector until the end of the line.
YAML supports three types of data structures. Object: A collection of key-value pairs, also known as a map (mapping)/hash (hashes)/dictionary (Dictionary) array: a set of sequential values, also known as sequences (sequence)/list (lists) of pure quantities (scalars): Single, non-re-divided values
These three data structures are described below. second, the object
A set of key-value pairs for an object, expressed using a colon structure.
Animal:pets
Switch to JavaScript as follows.
{animal: ' Pets '}
Yaml also allows another way to write all key-value pairs into an inline object.
Hash: {Name:steve, Foo:bar}
Switch to JavaScript as follows.
{hash: {name: ' Steve ', foo: ' Bar '}}
three, array
A set of lines at the beginning of a conjunction line that forms an array.
-Cat
-Dog
-Goldfish
Switch to JavaScript as follows.
[' Cat ', ' Dog ', ' goldfish ']
A child member of a data structure is an array, and you can indent a space below the item.
-
-Cat
-Dog
-Goldfish
Switch to JavaScript as follows.
[[' Cat ', ' Dog ', ' goldfish ']
Arrays can also be used in inline notation.
Animal: [Cat, Dog]
Switch to JavaScript as follows.
{animal: [' Cat ', ' Dog ']}
Four, composite structure
Objects and arrays can be used together to form a composite structure.
Languages:
-Ruby
-Perl
-Python
websites:
YAML:yaml.org
ruby:ruby-lang.org
Python:python.org
Perl:use.perl.org
Switch to JavaScript as follows.
{languages: [' Ruby ', ' Perl ', ' python '],
websites:
{YAML: ' yaml.org ',
Ruby: ' ruby-lang.org ',
Python: ' python.org ',
Perl: ' Use.perl.org '}}
Five, pure quantity
The pure quantity is the most basic, non-sub-divided value. The following data types belong to the pure amount of JavaScript.