Oggi vediamo come usare la libreria boost per leggere e filtrare il nostro json in modo semplice e intuitivo.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Includo le librerie #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> #include <iostream> int main(){ // Creo il root boost::property_tree::ptree root; // Carico il file json in tree boost::property_tree::read_json("test.json", root); // Metto un filtro nel json per ottenere quello che voglio std::string msg = root.get<std::string>("result.first_name"); // Stampo risultato std::cout << msg << std::endl; return 0; } |
File test.json
1 |
{"ok":true,"result":{"id":123456789,"first_name":"TelegramBot","username":"TelegramBot"}} |