1

My Json has some this sort of format for now (which can of course change later)

{key1 : value1,
 key2 : value2,
 key3 :{key31 : value31,
        key32 : value32,
        key33 : value33
        },

 key4 : {key41 : value41;
         key42:[ {key4a: value4a,
                  key4b: value4b,
                  key4c: {key4d: value 4d},
                          key4e: [v1 ,v2 ,v3]
                          } ,...can be more values here ]
        }
}

To traverse it I am using:

#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
#include "boost/foreach.hpp"


void traverse(boost::property_tree::ptree pt){
    using boost::property_tree::ptree;

    for (ptree::value_type &v : pt)
    {
        std::cout<<v.first<<" - "<<v.second.data()<<std::endl;


        if (v.second.size() >= 1){
            traverse(v.second);
        }
    }
}

With this I am able to touch each and every node of my Json. I am looking for a better approach to parse and store the Json Key:values now.

Abhishek
  • 33
  • 3
  • Are you bound to use boost ? a year back I have achieved similar functionality with a combination of maps, vectors and structs. And till now I have not seen any shortcomings (when compared to boost) in this way of doing it. – AdityaG Jul 25 '17 at 07:48
  • No I'm not bound so as to use boost. You see what I'm trying to achieve here is a proper data structure (a customized class) which can store data parsed from any valid json into it. – Abhishek Jul 25 '17 at 08:58

1 Answers1

0

I think you underestimate the ability of property tree. It's a great tool to store and access json and xml like infos and that's why it's introduced with json_parser and xml_parser in boost.

Here Accessing values using a boost::property_tree::string_path in the question you can see a working example of how to access values in property tree with path-like strings.

lin
  • 108
  • 2
  • 6
  • Is there any method in boost json_parser or by help of ptree ,by which I can get to know the type of value in a key-value pair, like whether the value is a string , an object, an array etc ? – Abhishek Jul 27 '17 at 06:44