Skip to content

Instantly share code, notes, and snippets.

@finalclass
Created August 20, 2012 16:36
Show Gist options
  • Save finalclass/3405616 to your computer and use it in GitHub Desktop.
Save finalclass/3405616 to your computer and use it in GitHub Desktop.
Simple boost (v1.48) json reading
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <string>
#include <set>
#include <exception>
#include <typeinfo> //for 'typeid' to work
int main(int argc, char** argv) {
using boost::property_tree::ptree;
using boost::property_tree::json_parser::read_json;
using std::exception;
using std::cout;
using std::endl;
using std::string;
boost::filesystem::path jsonDataFile = boost::filesystem::current_path() / "data.json";
cout << jsonDataFile << endl;
try {
ptree tree;
read_json (jsonDataFile.string(), tree);
auto v = tree.get<string>("name");
auto i = tree.get<int>("test");
cout << v << " " << i << endl;
} catch(exception &e) {
cout << e.what() << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment