c++ - 使用 Boost 的 JSON 数据

标签 c++ json boost

是否可以使用boost读取下面的数据?

{
  "ok": true,
  "result": [
    {
      "update_id": 1235285,
      "message": {
        "message_id": 2,
        "from": {
          "id": 3325446,
          "is_bot": false,
          "first_name": "Test",
          "language_code": "en-PH"
        },
        "chat": {
          "id": 12532541,
          "first_name": "Test Account",
          "type": "private"
        },
        "date": 152014521,
        "text": "Test Message"
      }
    }
  ]
}

最佳答案

您可以看到 linked post在评论中,

总而言之,您可以像下面那样从文件中读取 mfile.json :

    boost::property_tree::ptree pt;
    boost::property_tree::read_json("myfile.json", pt);

    print_contents( pt );

print_contents 是:

void print_contents( const boost::property_tree::ptree& pt)
{
    using boost::property_tree::ptree;

    for (const auto& x: pt )
    {
        std::cout << x.first << ": " << x.second.get_value<std::string>() << std::endl;
        print_contents(x.second);
    }
}

See Here


我本可以将其作为重复项关闭,但看起来没有“更好”的帖子来读取 json 文件

关于c++ - 使用 Boost 的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46054836/

相关文章:

c++ - 增加网关的负载能力

c++ - VS 2017 如何启用装饰?

javascript - 将 JSON 映射到 backbone.js 集合

javascript - 当编码为 javascript 时,我的 php 字符串变得很奇怪

c++ - 通过两个不同的可执行文件进行 IPC?

c++ - 在 Windows 7 下使用 VC++ 进行 LPT 错误 : "External component has thrown an exception."

android - Opencv4Android : How to use with C++

c# - 使用 Json.NET 序列化为 NDJSON

c++ - OSX 上 boost::program_options 和 C++11 的链接器错误

c++ - 如何初始化 boost 滚动窗口累加器?