繁体   English   中英

提升JSON解析器和IP地址

[英]Boost JSON parser and ip address

我想获取具有IP地址的node的子节点。 以下是我正在使用的参考JSON格式和代码。

{  
"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;        
}

如果节点包含“。”,您能否建议如何读取子级? ( IP地址 ) ? 在这里“ nodes.test”将起作用,但“ nodes.192.168.1.1”将不起作用,因为它包含“。” 作为字符串? 如何使其工作?

提前致谢。

文档

使用默认字符'.'以外的分隔符'.' ,您需要显式构造路径对象。 ptree的路径类型是string_path实例化,因此引用它的最简单方法是ptree::path_type 这样,您可以使用键中带有圆点的树[。]

在您的情况下:

boost::optional< ptree& > child = pt.get_child_optional(ptree::path_type("nodes/192.168.1.1", '/'));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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