使用Boost的JSON数据

时间:2017-09-05 12:19:10

标签: 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"
      }
    }
  ]
}

1 个答案:

答案 0 :(得分:1)

您可以在评论中看到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文件