Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在不知道子节点的情况下访问JSON值?_C++_Json_Boost_Boost Propertytree - Fatal编程技术网

C++ 如何在不知道子节点的情况下访问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")) {

尝试为我的应用程序动态创建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 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”
。或者class
ResponseJson
不是你的吗?我确实拥有这个类,但在这种情况下,我必须检查每个案例并相应地发送参数,但这不会简化事情,对于用例来说这不是一个好的解决方案,而是一个很好的解决方法。