Rapidjson迭代并获取复杂的JSON对象成员的值

时间:2018-08-11 03:08:48

标签: c++ json rapidjson

我有以下JSON对象

{  
   "prog":[  
      {  
         "iUniqueID":1,
         "bGroup":1,
         "inFiles":[  
            {  
               "sFileType":"Zonal Data 1",
               "bScenarioSpecific":0,
               "pos":{  
                  "x1":1555,
                  "y1":-375,
                  "x2":1879,
                  "y2":-432
               }
            },
            {  
               "sFileType":"Record File",
               "bScenarioSpecific":0,
               "pos":{  
                  "x1":1555,
                  "y1":-436,
                  "x2":1879,
                  "y2":-493
               }
            }
         ],
         "outFiles":[  
            {  
               "sFileType":"Record File 1",
               "bScenarioSpecific":1,
               "pos":{  
                  "x1":2344,
                  "y1":-405,
                  "x2":2662,
                  "y2":-462
               }
            }
         ]
      },
      {  
         "iUniqueID":2,
         "bGroup":1,
         "inFiles":[  
            {  
               "sFileType":"Matrix File 1",
               "bScenarioSpecific":0,
               "pos":{  
                  "x1":98,
                  "y1":-726,
                  "x2":422,
                  "y2":-783
               }
            },
            {  
               "sFileType":"Matrix File 2",
               "bScenarioSpecific":0,
               "pos":{  
                  "x1":98,
                  "y1":-787,
                  "x2":422,
                  "y2":-844
               }
            }
         ],
         "outFiles":[  
            {  
               "sFileType":"Record File 1",
               "bScenarioSpecific":1,
               "pos":{  
                  "x1":887,
                  "y1":-966,
                  "x2":1205,
                  "y2":-1023
               }
            }
         ]
      }
   ]
}

如何迭代访问“ inFiles”中对象的x1?或一般而言,如何使用Rapidjson访问存储在子数组和子对象中的值。这是我到目前为止所拥有的

const Value& prog = document["prog"];

assert(prog.IsArray());

for (rapidjson::Value::ConstValueIterator itr = prog.Begin(); itr != prog.End(); ++itr) {

}

我尝试了各种方法,但是我的代码无法编译,因此我觉得将其添加到问题描述中将很有效率。

2 个答案:

答案 0 :(得分:1)

这里是您可以遍历每个数组内的子数组的一种方法。使用range-for循环而不是迭代器。

rapidjson::Document doc;
doc.Parse(str); //the one shown in the question


for (auto const& p : doc["prog"].GetArray()) {
    std::cout << p["iUniqueID"].GetInt() << std::endl;
    for (auto const& in : p["inFiles"].GetArray()) {
        std::cout << in["sFileType"].GetString() << std::endl;
        std::cout << in["pos"]["x1"].GetInt() << std::endl;
    }
}

希望这会有所帮助。

答案 1 :(得分:0)

这才是最终工作

const Value& prog = d["prog"];
for (Value::ConstValueIterator p = prog.Begin(); p != prog.End(); ++p) {
    std:cout << (*p)["iUniqueID"].GetInt();
    const Value& inFiles = (*p)["inFiles"];
    for (Value::ConstValueIterator inFile = inFiles.Begin(); inFile != prog.End(); ++inFile) {
        std::cout << (*inFile)["sFileType"].GetString() << std::endl;
        std::cout << (*inFile)["pos"]["x1"].GetInt() << std::endl;
    }
}

帖子here的帮助很大。