Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.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
Json 如何在c++;_Json_Linux_C++11_Boost - Fatal编程技术网

Json 如何在c++;

Json 如何在c++;,json,linux,c++11,boost,Json,Linux,C++11,Boost,下面是我在代码中解析的json数组,用于填充参数列表数组数据,但不确定为什么它不工作,并且我无法访问嵌套数组参数列表的元素 { "packageList" :[ { "arcValue" : "Bond", "parameterList" : [ {"key1" : "value1", "key2" : "value2", "key3" : "va

下面是我在代码中解析的json数组,用于填充参数列表数组数据,但不确定为什么它不工作,并且我无法访问嵌套数组参数列表的元素

{
"packageList" :[    {
            "arcValue" : "Bond",
            "parameterList" : [ {"key1" : "value1",
                        "key2" : "value2",
                        "key3" : "value3"
                                 },

                        {"key4" : "value4",
                        "key5" : "value5",
                        "key6" : "value6"


                         }
                  ]


            },

                       {
            "arcValue" : "Bond1",
            "parameterList" : [ {"max" : "value1",
                        "rgb" : "value2",
                        "depth" : "value3"
                                },

                               {"max1" : "value4",
                                "max2" : "value5",
                                "max3" : "value6"

                                }
                          ]


            }

                 ]    

}           
下面是相同的代码片段

#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <map>
#include <iostream>
#include<list>
#include <vector>
using boost::property_tree::ptree;

struct Config {
    std::string name;
    std::list<std::vector<std::string>> parameters;
};

std::list<std::vector<std::string>> parse_config(std::string const& fname) {

    boost::property_tree::ptree pt;
    std::ifstream file(fname);
    boost::property_tree::read_json(file, pt);
    Config config;

    for (auto& v : pt.get_child("packageList"))
    {
        auto& node = v.second;

        config.name = node.get("arcValue", "");
        std::cout<<config.name;

       for(auto &param :node.get_child("parameterList"))
        {
          config.parameters.push_back({config.name,param.first,param.second.get_value("")});

         }   

    }

    return config.parameters;
}


int main() {

    std::list<std::vector<std::string>> vec = parse_config("sample2");

for (auto &v : vec)
{

    for (auto const &i : v)

        std::cout<<i<<std::endl;

}


}
/需要将与Bond1相关的参数信息从列表插入Bond1配置文件/

除此之外,在当前的实现中,Bond和Bond1都与参数一起插入到同一个列表中。有谁能建议这是正确的方法,还是可以用更好的方法实现?
根据我的要求,Bond和Bond1参数需要通过API接口插入到单独的配置文件中,但在当前的实现中,这两个参数都是组合在一起的。

您只需再迭代一次即可获得嵌套数组数据,以便填充嵌套数组。 下面是代码片段

for( auto &param : node.get_child("parameterList") )
{  
    for( const auto& itr : param.second )
    {
         config.parameters.push_back( {config.name,itr.second.get_value("")} );
    }
}  
以下是输出:

BondBond1Bond
value1
Bond
value2
Bond
value3
Bond
value4
Bond
value5
Bond
value6
Bond1
value1
Bond1
value2
Bond1
value3
Bond1
value4
Bond1
value5
Bond1
value6

谢谢你的更新。除此之外,正如我在关于设计策略的问题中所讨论的,插入应按以下顺序进行,包括四列和多行。(即,Bond”、“value1”、“value2”、“value3”)。您是否可以提出任何相同的设计建议。
for( auto &param : node.get_child("parameterList") )
{  
    for( const auto& itr : param.second )
    {
         config.parameters.push_back( {config.name,itr.second.get_value("")} );
    }
}  
BondBond1Bond
value1
Bond
value2
Bond
value3
Bond
value4
Bond
value5
Bond
value6
Bond1
value1
Bond1
value2
Bond1
value3
Bond1
value4
Bond1
value5
Bond1
value6