gpt4 book ai didi

c++ - Boost JSON解析器和IP地址

转载 作者:行者123 更新时间:2023-11-27 23:51:08 25 4
gpt4 key购买 nike

我想获取具有 IP 地址的节点的子节点。以下是我正在使用的引用 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”将不起作用,因为它包含“.”。作为字符串?如何让它工作?

提前致谢。

最佳答案

来自docs :

To use a separator character other than default '.', you need to construct a path object explicitly. The path type for a ptree is a string_path instantiation, so the easiest way to refer to it is ptree::path_type. This way you can use trees that have dots in their keys[.]

在你的情况下:

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

关于c++ - Boost JSON解析器和IP地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46364854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com