C++ Boost JSON解析器和ip地址
我想获取具有ip地址的节点的子节点。 下面是我使用的参考JSON格式和代码C++ Boost JSON解析器和ip地址,c++,json,boost,C++,Json,Boost,我想获取具有ip地址的节点的子节点。 下面是我使用的参考JSON格式和代码 { "nodes":{ "192.168.1.1": { "type":"type1", "info":"info1", "error":"error1" }, "192.168.1.2":{ "type":"type2", "info":"info2", "error":"error2" },
{
"nodes":{
"192.168.1.1": {
"type":"type1",
"info":"info1",
"error":"error1"
},
"192.168.1.2":{
"type":"type2",
"info":"info2",
"error":"error2"
},
"test":{
"type":"type2",
"info":"info2",
"error":"error2"
}
}
}
下面是阅读上述json数据的参考代码
using boost::property_tree::ptree;
ptree pt;
std::string ttr("test.json");
read_json(ttr, pt);
BOOST_FOREACH(ptree::value_type &v, pt.get_child("nodes"))
{
std::string key_ = v.first.data();
std::string val_ = v.second.data();
boost::optional< ptree& > child = pt.get_child_optional( "nodes.192.168.1.1" );
if( !child )
{
std::cout << "Child Node Missing.............." << std::endl; //Always shows Node Missing. How to deal with "." as key ?
}
else
std::cout << "Child Node Not Missing.............." << std::endl;
}
使用boost::property_tree::ptree;
ptree-pt;
std::string ttr(“test.json”);
读json(ttr,pt);
BOOST_FOREACH(ptree::value_type&v,pt.get_child(“节点”))
{
std::string key_u=v.first.data();
std::string val_uz=v.second.data();
boost::optionalchild=pt.get\u child\u optional(“nodes.192.168.1.1”);
如果(!child)
{
标准::cout来自:
要使用除默认值以外的分隔符。
,您需要显式构造一个路径对象。ptree
的路径类型是一个string\u path
实例化,因此引用它的最简单方法是ptree::path\u type
。这样,您可以使用键中有点的树[。]
就你而言:
boost::optional< ptree& > child = pt.get_child_optional(ptree::path_type("nodes/192.168.1.1", '/'));
boost::optionalchild=pt.get_child_optional(ptree::path_类型(“nodes/192.168.1.1”,“/”);
来自:
要使用除默认值以外的分隔符。,您需要显式构造一个路径对象。ptree
的路径类型是一个string\u path
实例化,因此引用它的最简单方法是ptree::path\u type
。这样,您可以使用键中有点的树[。]
就你而言:
boost::optional< ptree& > child = pt.get_child_optional(ptree::path_type("nodes/192.168.1.1", '/'));
boost::optionalchild=pt.get_child_optional(ptree::path_类型(“nodes/192.168.1.1”,“/”);