0

I have the following array of anonymous JSON data and used boost property tree to read the data. I am able to read the data and wondering if there is any better to parse the data using boost property tree.

Here is the data set

[{"id": "1","timestamp": 1509493641,"heartrate": 72},
 {"id": "2","timestamp": 1509493642,"heartrate": 74}]

Here is the I code to extract the data and it works.

// boost library headers
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/algorithm/string/erase.hpp>

int main()
{
    // Short alias for this namespace
    namespace pt = boost::property_tree;
    // read the JSON array data using boost property tree
    pt::ptree jsonData;

// Load the json file in this ptree
pt::read_json(inputFile, jsonData);

for(auto v = jsonData.begin(); v != jsonData.end(); ++v) 
{
    const std::string id = v->second.get_child("id").get_value<std::string>();
    const int heartrate= v->second.get_child("heartrate").get_value<int>();
}
return 0;

}

Is there any other better way to do this using boost proprty tree library?

pablo285
  • 2,460
  • 4
  • 14
  • 38
Jolly Jose
  • 84
  • 5
  • Please clarify - what do you mean by better? What is wrong with the code you are showing. Are you having any problems with it? – pablo285 Jul 30 '18 at 13:36
  • There is no issue in parsing the JSON data. Most of the JSON data are named, but this one is anonymous JSON. I wanted to know if there is any elegant way to read child wihtout calling v->second.get_child(). – Jolly Jose Aug 09 '18 at 21:52

0 Answers0