i am new to Boost libraries and quite new to programming also. I am trying to read JSON file which is created by hand using boost libraries and boost property tree in C++. I need to use boost property tree to read elements from JSON file
this is my JSON file looks like Example.json
{
"Shapes":{
"Square":{
"dimension1":1234,
"dimension2":5678
},
"Rectangle":{
"dimension1":4321,
"dimension2":8765
},
"Triangle":{
"dimension1":2468,
"dimension2":8642
}
}
}
Here i need to read this JSON file using boost property tree using C++ code and boost::property_tree::ptree pt. If I pass Square I am able to read all the dimensions present in that (for eg. "dimension1":1234,"dimension2":5678) and same for Rectangle and Triangle. Please can anyone suggest how to solve this.
I have written below code but it is not working
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <iostream>
#include <string>
using namespace std;
int main()
{
namespace pt = boost::property_tree;
pt::ptree root; // Create a root
pt::read_json("Example.json", root); // Load the json file in this ptree
std::vector< std::pair<std::string, std::string> > Square;
for (pt::ptree::value_type& result : root.get_child("Square"))
{
// Get the label of the node
std::string label = Square.first;
std::string content = Square.second.data();
results.push_back(std::make_pair(label, content));
cout << Square.second.data();
}
return 0;
}