简体   繁体   中英

How do I store a JSON key values (which I have parsed with cpp boost) in relevant data structure?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM