0

I am parsing below sample file in below code snippet.

{"requestType":"INVOCATION", "hostName":"localhost", "serviceName":"bucky", "serviceType":"DISCRETE", "serviceParameters":"sampleData", "servicesList" :[ { "serviceName" : "ABC", "serviceParameters" : {"para1" : "value1", "para2" : "value2", "para3" : "value3"} }, {"serviceName" : "CBA", "serviceParameters" : { "para1" : "value90", "para2" : "value", "para3" : "value"} } ], "dataTransferMode":null }

code snippet:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <sstream>
#include <iostream>
#include <boost/foreach.hpp>
#include <map>
using boost::property_tree::ptree;
using namespace std;

void print(boost::property_tree::ptree const& pt,vector <string> &service_list, map <string,string> &service_param)
{   
    using boost::property_tree::ptree;
    ptree::const_iterator end = pt.end();
    string value;
    for (ptree::const_iterator it = pt.begin(); it != end; ++it) {
               //cout<<it->first<<":"<<it->second.get_value<std::string>()<<endl;

                if(it->first == "serviceName")
                {
                     value = it->second.get_value<std::string>();
                    service_list.push_back(it->second.get_value<std::string>());
                }

                 if(it->first == "para1")
                {   

                    service_param[it->first]=it->second.get_value<std::string>();

                }

                 if(it->first == "para2" )
                {  
                    service_param [it->first]=it->second.get_value<std::string>();

                }

                 if(it->first == "para3" )
                {   
                     service_param [it->first]=it->second.get_value<std::string>();
                }

        print(it->second,service_list,service_param);

    }
}

int main() {

vector <string> service_list; 
map <string,string> service_param_rgbd;
map <string,map<string,string>> map_name;
std::ifstream file( "sample" );
    std::stringstream ss;

    if ( file )
    {
        ss << file.rdbuf();
        cout<<"done";
        file.close();
    }

    boost::property_tree::ptree pt;
    boost::property_tree::read_json(ss, pt);

try
{
    BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("servicesList"))
        {
            assert(v.first.empty()); // array elements have no names
            print(v.second,service_list,service_param_rgbd);

        }

 for(auto itr: service_list )
       {

        cout<<itr<<endl;
       } 

for(auto itr: service_param_rgbd )
       {
       if(find(service_list.begin(),service_list.end(),"ABC")!=service_list.end())
        {
       map_name["ABC"][itr.first]=itr.second;
        }

       } 

   for(auto &i : map_name)
    {

    for(auto &j:i.second)
           {
             cout<<j.first <<":"<<j.second<<endl;

             }

     }

return EXIT_SUCCESS;

}

    catch (std::exception const& e)
    {
        std::cerr << e.what() << std::endl;
    }
    return EXIT_FAILURE
}

In above code i need to populate vector and map in order to have mapping between service name and service parameter and for the same map > map_name needs to be created.

so in current scenario inside loop itself i want to create the nested map so that i will have mapping of service name and corresponding parameters . As per below code snippet here in outer loop i->first will iterate the service name and inner loop will provide me corresponding parameters (para1=>value1,para2=>value2) . This way both service name and corresponding parameters can be tied in one map.

for(auto &i : map_name) {
    for(auto &j:i.second)
           {
             cout<<j.first <<":"<<j.second<<endl;

             }

     }

Can anybody let me know the efficient approach for the same as in this approach :

(a) Separate nested map needs to be created for each of the services. (b) same param name for both services name will not work and different-2 services with same param name can't be differentiated.

2
Contributors
2
Replies
12
Views
1 Year
Discussion Span
Last Post by can-mohan
0

Thanks.yes i got answer from stackoverflow and full satisfied with this.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.