# Person information
person:
name: John Doe
age: 30
address:
street: 123 Main St
city: Somewhere
country: USA
hobbies:
- reading
- hiking
- coding
# A list of items
items:
- apple
- banana
- orange
键值对(Mapping)
键值对使用 :
进行分隔,键和值之间用空格分开。
name: John Doe
age: 30
列表(Sequence)
列表元素前使用 -
符号进行标记。
fruits:
- apple
- banana
- cherry
嵌套结构
YAML 使用缩进来表示嵌套结构。通常使用2个空格作为缩进。
person:
name: Alice
age: 28
address:
street: 123 Main St
city: Wonderland
注释(Comments)
注释以 #
开头,注释内容会被忽略。
# This is a comment
name: John Doe # Inline comment
多行字符串
使用 |
或 >
来表示多行字符串。
description: |
This is a multi-line
string that keeps line breaks.
note: >
This is a folded
multi-line string that
turns into a single line.
复杂数据类型
YAML 支持复杂数据类型,如时间、日期、布尔值等。
date: 2024-12-25
boolean: true
yaml-cpp
sudo apt install libyaml-cpp-dev
git clone <https://github.com/jbeder/yaml-cpp.git>
cd yaml-cpp
mkdir build
cd build
cmake ..
make && make install
几种YAML::NodeType: