yaml-cpp 解析嵌套映射和序列错误
yaml-cpp parsing nested maps and sequences error
我正在尝试解析一个带有嵌套映射和序列的文件,它看起来有点像这样
annotations:
- dodge:
type: range based
attributes:
start_frame:
frame_number: 25
time_stamp: 2017-10-14 21:59:43
endframe:
frame_number: 39
time_stamp: 2017-10-14 21:59:45
distances:
- 2
- 5
- 6
我收到一条错误消息,提示“确保节点存在”。下面是我的示例代码。
YAML::Node basenode = YAML::LoadFile(filePath);
const YAML::Node& annotations = basenode["annotations"];
RangeBasedType rm;
for (YAML::const_iterator it = annotations.begin(); it != annotations.end(); ++it)
{
const YAML::Node& gestures = *it;
std::string type = gestures["type"].as<std::string>(); // this works
rm.setGestureName(type);
if (type == "range based")
{
const YAML::Node& attributes = gestures["attributes"];
for (YAML::const_iterator ti = attributes.begin(); ti != attributes.end(); ++ti)
{
const YAML::Node& frame = *ti;
if (frame["start_frame"]) // this fails saying it is not a valid node
{
std::cout << frame["frame_number"].as<int>();
rm.setStartFrame(frame["frame_number"].as<int>());
}
}
}
}
我希望从节点 start_frame 和 end_frame 获取 frame_number。我已经检查了 YAML 格式的有效性。为什么这不起作用的任何原因?
这个循环:
for (YAML::const_iterator ti = attributes.begin(); ti != attributes.end(); ++ti)
正在遍历地图节点。因此,迭代器指向 key/value 对。您的下一行:
const YAML::Node& frame = *ti;
取消引用它作为一个节点。相反,您需要查看其 key/value 个节点:
const YAML::Node& key = ti->first;
const YAML::Node& value = ti->second;
yaml-cpp
允许迭代器指向节点和 key/value 对,因为它可以是映射或序列(或标量),并且它是作为单个 C++ 类型实现的。
我正在尝试解析一个带有嵌套映射和序列的文件,它看起来有点像这样
annotations:
- dodge:
type: range based
attributes:
start_frame:
frame_number: 25
time_stamp: 2017-10-14 21:59:43
endframe:
frame_number: 39
time_stamp: 2017-10-14 21:59:45
distances:
- 2
- 5
- 6
我收到一条错误消息,提示“确保节点存在”。下面是我的示例代码。
YAML::Node basenode = YAML::LoadFile(filePath);
const YAML::Node& annotations = basenode["annotations"];
RangeBasedType rm;
for (YAML::const_iterator it = annotations.begin(); it != annotations.end(); ++it)
{
const YAML::Node& gestures = *it;
std::string type = gestures["type"].as<std::string>(); // this works
rm.setGestureName(type);
if (type == "range based")
{
const YAML::Node& attributes = gestures["attributes"];
for (YAML::const_iterator ti = attributes.begin(); ti != attributes.end(); ++ti)
{
const YAML::Node& frame = *ti;
if (frame["start_frame"]) // this fails saying it is not a valid node
{
std::cout << frame["frame_number"].as<int>();
rm.setStartFrame(frame["frame_number"].as<int>());
}
}
}
}
我希望从节点 start_frame 和 end_frame 获取 frame_number。我已经检查了 YAML 格式的有效性。为什么这不起作用的任何原因?
这个循环:
for (YAML::const_iterator ti = attributes.begin(); ti != attributes.end(); ++ti)
正在遍历地图节点。因此,迭代器指向 key/value 对。您的下一行:
const YAML::Node& frame = *ti;
取消引用它作为一个节点。相反,您需要查看其 key/value 个节点:
const YAML::Node& key = ti->first;
const YAML::Node& value = ti->second;
yaml-cpp
允许迭代器指向节点和 key/value 对,因为它可以是映射或序列(或标量),并且它是作为单个 C++ 类型实现的。