[英]Retrieve the whole yaml object
鑒於此 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;
}
我能夠檢索主鍵: CR
和OL
。 對於每一個,我還需要檢索整個 object:
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
獲取上述字符串的正確語法是什么?
(*i).first
i).first 是鍵, (*i).second
是它的值。
因此, (*i)
就是您所說的整個 object (鍵 + 值)。 它根本不是字符串,這就是為什么您無法通過.as<QString>()
檢索它的原因。 每個鍵和值都是YAML::Node
就像root一樣,您只能在鍵上執行.as<QString>()
因為它是一個字符串。 在值上,您可以執行(*i).second["cmd"].as<QString>()
等。
如果您希望該值是字符串而不是嵌套的 YAML 結構,則不應將其輸入為嵌套的 YAML 結構。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.