使用yaml cpp解析yaml
Parsing yaml with yaml cpp
我正在尝试使用yaml-cpp解析一个yaml。这是我的网址:
---
configuration:
- height: 600
- widht: 800
- velocity: 1
- scroll: 30
types:
- image: resources/images/grass.png
name: grass
- image: resources/images/water.png
name: water
version: 1.0
当我做
YAML::Node basenode = YAML::LoadFile("./path/to/file.yaml");
int height;
if(basenode["configuration"])
if(basenode["configuration"]["height"]
height = basenode["configuration"]["height"].as<int>();
else
cout << "The node height doesn't exist" << endl;
else
cout << "The node configuration doesn't exist" << endl;
我得到消息:"节点高度不存在"。如何访问该字段(以及其他字段)
非常感谢!
您在-
中使用的语法创建数组元素。这意味着您正在创建(JSON表示法):
{configuration: [{height: 600}, {width: 800}, {velocity: 1}, {scroll: 30}]}
但是你想要的是:
{configuration: {height: 600, width: 800, velocity: 1, scroll: 30}}
幸运的是,解决方法很简单。只需删除错误的-
字符:
---
configuration:
height: 600
width: 800
velocity: 1
scroll: 30
types:
- image: resources/images/grass.png
name: grass
- image: resources/images/water.png
name: water
version: 1.0
请注意,我还修正了width的错别字,并删除了version: 1.0
如果你想知道如何访问你的配置,你必须做一个数组访问:
int height = basenode["configuration"][0]["height"].as<int>();
int height = basenode["configuration"][1]["width"].as<int>();
显然,如果你真的想要这样,这将是相当讨厌的,因为这意味着你不再使用键,但必须有顺序问题或重新处理配置,以摆脱数组级别。
相关文章:
- 继承函数的重载解析
- 基类中的函数名称解析
- 提升精神:解析布尔表达式并简化为规范范式
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- gcc和c++17的过载解析失败
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 使用 yaml-cpp 解析 YAML 文件
- 解析序列/映射节点时出现无效的 yaml 节点错误
- YAML-cpp 解析嵌套映射和序列错误
- YAML-CPP 未解析的外部符号错误
- 如何使用 yaml-cpp 解析文件
- YAML未解析的外部符号(YAML-cpp)
- 使用 yaml-cpp 解析 json/yaml 数组
- 适用于cocos2d-x的高效稳定的YAML解析器
- Yaml-cpp 解析不起作用 冒号后缺少空间
- 解析YAML !!时间戳(日期时间)与yaml-cpp
- 使用yaml cpp解析yaml
- 如何防止yaml-cpp解析器剥离所有注释?
- Yaml-cpp解析混合数组/字符串节点时出现问题
- 使用YAML -cpp解析YAML期间的内存消耗