使用Boost的JSON数据

JSON Data using Boost

本文关键字:数据 JSON Boost 使用      更新时间:2023-10-16

是否可以使用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"
      }
    }
  ]
}

您可以在评论中看到链接的帖子

总而言之,您可以喜欢从文件中读取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文件