Below is the json array which i am parsing in my code in order to populate the parameterList array data but not sure why it is not working and i am not able to access the elements of nested array paramList.
{
"packageList" :[ {
"arcValue" : "Bond",
"parameterList" : [ {"key1" : "value1",
"key2" : "value2",
"key3" : "value3"
},
{"key4" : "value4",
"key5" : "value5",
"key6" : "value6"
}
]
},
{
"arcValue" : "Bond1",
"parameterList" : [ {"max" : "value1",
"rgb" : "value2",
"depth" : "value3"
},
{"max1" : "value4",
"max2" : "value5",
"max3" : "value6"
}
]
}
]
}
Below is the code snippet for the same:
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <map>
#include <iostream>
#include<list>
#include <vector>
using boost::property_tree::ptree;
struct Config {
std::string name;
std::list<std::vector<std::string>> parameters;
};
std::list<std::vector<std::string>> parse_config(std::string const& fname) {
boost::property_tree::ptree pt;
std::ifstream file(fname);
boost::property_tree::read_json(file, pt);
Config config;
for (auto& v : pt.get_child("packageList"))
{
auto& node = v.second;
config.name = node.get("arcValue", "");
std::cout<<config.name;
for(auto ¶m :node.get_child("parameterList"))
{
config.parameters.push_back({config.name,param.first,param.second.get_value("")});
}
}
return config.parameters;
}
int main() {
std::list<std::vector<std::string>> vec = parse_config("sample2");
for (auto &v : vec)
{
for (auto const &i : v)
std::cout<<i<<std::endl;
}
}
Basically in above code all the param related with arcValue (i.e Bond,Bond1)needs to be inserted in list of vector and later on same needs to be inserted in config file through API interface. /Bond related information needs to inserted from list of vector to Bond config file/
structure.svc.parameters.push_back({"Bond","value1","value2","value3"});
structure.svc.parameters.push_back({"Bond","value4","value5","value6"});
/Bond1 related parameter information needs to inserted from list to Bond1 config file/
structure.svc.parameters.push_back({"Bond1","value1","value2","value3"});
structure.svc.parameters.push_back({"Bond1","value4","value5","value6"});
Apart from this in current implementation both Bond and Bond1 are being inserted in same list along with parameters. Can anybody suggest is this right approach or same can be implemented with better way ? As per my requirement Bond and Bond1 parameters needs to be inserted in separate config file through API interface but in current implementation both are clubbed altogether.
Aucun commentaire:
Enregistrer un commentaire