繁体   English   中英

yams-cpp嵌套序列返回映射而不是值

[英]yams-cpp nested sequence returning map instead of value

我正在尝试解析以下config.yaml文件。

config.yaml

foo:
  bar:
    baz: [1, 2, 3, 4]
    bam: "some_string_value"

test.cpp

YAML::Node configObj = YAML::LoadFile("cfig.yaml"); // loads file just fine

YAML::Node fooObj = configObj["foo"]; // this Node object is a Map

// iterate over foo node to get bar node
for( auto it = fooObj.begin(); it != fooObj.end(); ++it){
    YAML::Node barMap = it->second; // this Node object is a Map

    // iterate over bar node to get bad node
    for( auto i = barMap.begin(); i != barMap.end(); ++i){
        YAML::Node bazMap = i->second; // this node is a sequence

        for( std::size_t i=0; i<bazMap.size(); i++         
            auto index = bazMap[i].as<int>(); // <<< This is the problem
        }
    }
}

据我所知,问题是我期望index是一个int,但bazMap[i].as<int>()我期望第一个是1,第二个是2, bazMap[i].as<int>() 。我得到的是相反, bazMap[i].as<int>()的类型为map。 我在这里的理解中缺少什么?

谢谢,

布鲁斯

更新答案是我在嵌套循环中停了下来。

我找到的答案在于,我不清楚自己得到了什么。

config.yaml文件中,我期望以下内容:

foo.Type() == Map
bar.Type() == Sequence
baz.Type() == Sequence

但是我得到的是:

foo.Type() == Map
bar.Type() == Map
baz.Type() == Sequence

解决此问题所需要做的就是更改配置文件。

foo:
  bar:
    - baz: [1, 2, 3, 4]
    - bam: "some_string_value"

然后它完全按照我的预期进行了解析。

因此,问题出在配置文件的格式,而不是解析逻辑中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM