0

鉴于此 yaml:

{CR: {cmd: fade, color: blue, panel: 0, value: 30, fout: 0.5, fint: 5},OL: {cmd: text, value: Blu at 30% on all, color: white, time: 5, position: [540,100], size: 50}}

使用此代码:

bool SEMTools::decodeYaml(QString yaml)
{
    try
    {
        YAML::Node root = YAML::Load(yaml.toStdString().c_str());
        YAML::Node::iterator i;
        for (i = root.begin(); i != root.end(); i++)
        {
            qDebug() << (*i).first.as<QString>();
        }
        return true;
    }
    catch (YAML::TypedBadConversion<QString> const &e)
    {
        qDebug() << e.what();
    }

    return false;
}

我能够检索主键:CROL. 对于每一个我还需要检索整个对象:

CR: {cmd: fade, color: blue, panel: 0, value: 30, fout: 0.5, fint: 5}

OL: {cmd: text, value: Blu at 30% on all, color: white, time: 5, position: [540,100], size: 50}

我试过:

qDebug() << (*i).as<QString>();

但我的应用程序因此错误而崩溃:

terminate called after throwing an instance of 'YAML::InvalidNode'
  what():  invalid node; this may result from using a map iterator as a sequence iterator, or vice-versa

获取上述字符串的正确语法是什么?

4

1 回答 1

1

(*i).first是关键,(*i).second是它的价值。

因此,(*i)就是您所说的整个对象(键+值)。它根本不是一个字符串,这就是为什么你不能通过.as<QString>(). 每个 key 和 value 都是一个YAML::Node就像root一样,你.as<QString>()只能在 key 上做,因为它是一个字符串。在价值上,你可以做(*i).second["cmd"].as<QString>()等。

如果您希望值是字符串而不是嵌套的 YAML 结构,则不应将其输入为嵌套的 YAML 结构。

于 2021-12-19T13:19:56.647 回答