Boost JSON解析器和IP地址
Boost JSON parser and ip address
我想获取具有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"不起作用,因为它包含"。作为字符串?如何使其工作?
预先感谢。
来自文档:
要使用默认
'.'
以外的分离器字符,您需要明确构造路径对象。ptree
的路径类型是string_path
的实例化,因此最简单的参考方法是ptree::path_type
。这样,您可以使用键中有点的树[。]
在您的情况下:
boost::optional< ptree& > child = pt.get_child_optional(ptree::path_type("nodes/192.168.1.1", '/'));
相关文章:
- 如何使用 Boost Asio 在 Android 上获取我的本地 udp IP 地址?
- C++ / Qt:如何检测主机名或IP地址何时引用当前系统?
- 使用 C++将 IP 地址附加到字符*
- 使用全局 IP 地址时,C++ winsock 2 应用程序中的代码是否必须更改?
- Rabbitmq 将本地主机更改为 IP 地址C++
- C++ - 使用代码块从文件中读取 IP 地址
- C 使用from_string创建IP地址的C 导致lambda错误
- 如何在解析过程中区分 IP 地址和域名
- 如何使用 asio 库获取 IP 地址的主机名
- IP地址匹配过滤功能
- 如何将C++ ulong 4 字节 IP 地址正确转换为 C# 类型
- 如何在NS3中设置节点的IP地址和数据?
- 如何从活动的 udp 连接获取远程 IP 地址和端口
- 无符号的 int 到 IP 地址字符串,不带 itoa/to_string/boost
- 如何在FD_SET内获取套接字的IP地址
- 使用Boost库中的PC中获取我的以太网设备IP地址的列表
- 使用静态IP地址时,ESP32 httpclient连接拒绝了
- C++检查有效的 IP 地址或 IP
- 如何使用 Poco::Net::HTTPSClientSession 绑定特定的源 IP 地址
- 如何在Windows上绕过主机文件获取真实的IP地址