我有一个包含一些 JSON 内容的文件,如下所示:
{
"frame":
{
"id": "0",
"points":
[
[ "0.883", "0.553", "0" ],
[ "0.441", "0.889", "0" ],
]
},
"frame":
...
}
如何使用 C++ 和 Boost ptree 解析 double 组的值?
最佳答案
使用迭代器,Luke。
首先,你必须解析文件:
boost::property_tree::ptree doc;
boost::property_tree::read_json("input_file.json", doc);
...现在,因为您似乎在顶级字典中有多个“框架”键,您必须遍历它们:
BOOST_FOREACH (boost::property_tree::ptree::value_type& framePair, doc) {
// Now framePair.first == "frame" and framePair.second is the subtree frame dictionary
}
遍历行和列是一样的:
BOOST_FOREACH (boost::property_tree::ptree::value_type& rowPair, frame.get_child("points")) {
// rowPair.first == ""
BOOST_FOREACH (boost::property_tree::ptree::value_type& itemPair, rowPair.second) {
cout << itemPair.second.get_value<std::string>() << " ";
}
cout << endl;
}
我没有测试代码,但这个想法会奏效:-)
关于c++ - 如何使用 C++ Boost 解析 JSON 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17124652/