c++ - 如何使用 boost::property_tree 解析带根数组的 JSON

标签 c++ json boost boost-propertytree

如何使用 Boost.PropertyTree 从以数组为根节点的 JSON 中获取数据?

[
  {
      "ID": "cc7c3e83-9b94-4fb2-aaa3-9da458c976f7",
      "Type": "VM"
  }
]

最佳答案

数组元素只是带有属性树名为“”的键的值:

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

Live On Coliru

#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";
        }
    }
}

关于c++ - 如何使用 boost::property_tree 解析带根数组的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26937016/

相关文章:

PS3 的 C++ 内存编写

java - Volley每次从SERVER而不是缓存获取数据

json - Powershell:l 解析嵌套的 JSON

c++ - xcode 6.4 中的链接器警告

c++ - 分配给不同类型时,返回值优化是否有效?

c++ - Arduino Web 客户端类不工作

c++ - QString 格式化将不起作用 qDebug

json - 是否可以让 YQL 在单个请求中从站点返回多个 JSON 结果?

c++ - Boost.花 : How to check if function has specialisation for a certain type?

c++ - BOOST::CRC使用process.block()方法创建校验和