Below are the steps i followed to fetch the values from the json file. And i am failing to fetch the boolean values from the json file.
Input Json:
{"Bases":[{"mnemonic":"ADIS.LA.01","relay":true},{"mnemonic":"ALEX.LA.01",,"relay":true}]}
In the code below Iam
- Opening the json file
- Set the root element and start travering the childtree under this root element(Bases)
- Fetch the values of each tag and save it to appropriate variable type
Code:
ReadJsonFile()
{
using boost::property_tree::ptree;
const boost::property_tree::ptree& propTree
boost::property_tree::read_json(ss, pt);
const std::string rootElement = "Bases";
boost::property_tree::ptree childTree;
bool m_relay;
try
{
/** get_child - Get the child at the given path, or throw @c ptree_bad_path. */
childTree = propTree.get_child(rootElement);
}
catch (boost::property_tree::ptree_bad_path& ex)
{
return false;
}
BOOST_FOREACH(const boost::property_tree::ptree::value_type &v, propTree.get_child(rootElement)){
string vID;
for (ptree::const_iterator subTreeIt = v.second.begin(); subTreeIt != v.second.end(); ++subTreeIt) {
if (subTreeIt->first == "mnemonic")
{
// Get the value string and trim the extra spaces, if any
vID = boost::algorithm::trim_copy( subTreeIt->second.data() );
}
if (subTreeIt->first == "relay")
{
m_relay = boost::algorithm::trim_copy(subTreeIt->second.data());
}
}
}
}
Error:
error: cannot convert ‘std::basic_string, std::allocator >’ to ‘bool’ in assignment
Apparently the json file has a boolean value set to the tag "relay":true and the above code treats it as a string but not bool
If i change
bool m_relay;
tostd::string m_relay;
The code works fine, but bool type is failing to compile. Am i missing on something? Please advice .
Thanks
Aucun commentaire:
Enregistrer un commentaire