【问题标题】:Rapidjson Iterating over and Getting Values of Complex JSON Object MembersRapidjson 迭代并获取复杂 JSON 对象成员的值
【发布时间】:2018-08-11 03:08:48
【问题描述】:

我有以下 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) {

}

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

【问题讨论】:

  • 请提供minimal reproducible example 以及您收到的确切编译命令和错误消息。此外,如果代码无法编译,则 JSON 的内容也无关紧要。
  • jq 怎么样? cat f.json | jq .prog[].inFiles[].pos.x1

标签: c++ json rapidjson


【解决方案1】:

这是一种迭代每个数组内的子数组的方法。使用 ranged-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;
    }
}

希望这会有所帮助。

【讨论】:

  • 感谢您的帮助。现在,该项目正在使用 Visual Studio 2010 进行编译,因此当我尝试上述操作时,我收到错误“无法推断 'auto' 类型(需要初始化程序)”。关于如何使用迭代器做同样的事情的任何想法?
  • 在我可以编辑之前,另一个人使用迭代器发布了相同的代码。你可以检查一下。
【解决方案2】:

这就是最终的工作

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 的帖子帮助很大。

【讨论】:

    猜你喜欢
    • 2020-05-14
    • 2011-05-23
    • 2021-03-06
    • 2018-08-19
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    相关资源
    最近更新 更多