C++ 如何在不知道子节点的情况下访问JSON值?
尝试为我的应用程序动态创建JSON,但我收到的JSON并没有保持不变C++ 如何在不知道子节点的情况下访问JSON值?,c++,json,boost,boost-propertytree,C++,Json,Boost,Boost Propertytree,尝试为我的应用程序动态创建JSON,但我收到的JSON并没有保持不变 std::string ResponseJson::getValue(std::string filter, std::string filterName, std::string jsonIndex) { BOOST_FOREACH(boost::property_tree::ptree::value_type & arrayElement, root.get_child("message")) {
std::string ResponseJson::getValue(std::string filter, std::string filterName, std::string jsonIndex)
{
BOOST_FOREACH(boost::property_tree::ptree::value_type & arrayElement, root.get_child("message"))
{
std::string value = arrayElement.second.get<std::string>(filter);
if (value == filterName)
{
return arrayElement.second.get<std::string>(jsonIndex);
}
}
return "";
}
std::string ResponseJson::getValue(std::string过滤器、std::string过滤器名称、std::string jsonIndex)
{
BOOST\u FOREACH(BOOST::property\u tree::ptree::value\u type&arrayElement,root.get\u child(“消息”))
{
std::string value=arrayElement.second.get(过滤器);
如果(值==filterName)
{
返回arrayElement.second.get(jsonIndex);
}
}
返回“”;
}
上面的代码片段工作得很好,但是
message
是可以在不同的JSON中更改的子函数,因此如何使此函数成为通用函数?如果可以更改函数getValue
的签名,请将子函数的名称作为参数传递,并用该参数替换字符串literal“message”
。或者classResponseJson
不是你的吗?我确实拥有这个类,但在这种情况下,我必须检查每个案例并相应地发送参数,但这不会简化事情,对于用例来说这不是一个好的解决方案,而是一个很好的解决方法。