【发布时间】:2015-01-12 06:13:09
【问题描述】:
如何使用 Boost.PropertyTree 从以数组为根节点的 JSON 中获取数据?
[
{
"ID": "cc7c3e83-9b94-4fb2-aaa3-9da458c976f7",
"Type": "VM"
}
]
【问题讨论】:
标签: c++ json boost boost-propertytree
如何使用 Boost.PropertyTree 从以数组为根节点的 JSON 中获取数据?
[
{
"ID": "cc7c3e83-9b94-4fb2-aaa3-9da458c976f7",
"Type": "VM"
}
]
【问题讨论】:
标签: c++ json boost boost-propertytree
数组元素只是属性树的键名为“”的值:
for (auto& array_element : pt) {
for (auto& property : array_element.second) {
std::cout << property.first << " = " << property.second.get_value<std::string>() << "\n";
}
}
打印
ID = cc7c3e83-9b94-4fb2-aaa3-9da458c976f7
Type = VM
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <iostream>
using namespace boost::property_tree;
int main()
{
std::istringstream iss(R"([
{
"ID": "cc7c3e83-9b94-4fb2-aaa3-9da458c976f7",
"Type": "VM"
}
]
)");
ptree pt;
json_parser::read_json(iss, pt);
for (auto& array_element : pt) {
for (auto& property : array_element.second) {
std::cout << property.first << " = " << property.second.get_value<std::string>() << "\n";
}
}
}
【讨论】:
pt.get<std::string>("key"); 中的内容,因为根数组没有键,我必须在数组上使用迭代,对吗?