C++ Rapidjson迭代并获取复杂JSON对象成员的值
我有以下JSON对象C++ Rapidjson迭代并获取复杂JSON对象成员的值,c++,json,rapidjson,C++,Json,Rapidjson,我有以下JSON对象 { "prog":[ { "iUniqueID":1, "bGroup":1, "inFiles":[ { "sFileType":"Zonal Data 1", "bScenarioSpecific":0, "pos":{ "x1
{
"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
}
}
]
}
]
}
如何迭代访问“内嵌”中对象的x1?或者通常,如何使用rapidjson访问存储在子数组和子对象中的值。这是我到目前为止所拥有的
const Value& prog = document["prog"];
assert(prog.IsArray());
for (rapidjson::Value::ConstValueIterator itr = prog.Begin(); itr != prog.End(); ++itr) {
}
我已经尝试了很多方法,但是我的代码没有编译,因此我觉得将其添加到问题描述中会很有成效。下面是一种可以迭代每个数组中的子数组的方法。使用循环范围而不是迭代器
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;
}
}
rapidjson::文档文档;
文件解析(str)//问题中显示的那个
对于(自动常量和p:doc[“prog”].GetArray()){
std::cout以下是最终有效的方法
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;
}
}
const Value&prog=d[“prog”];
对于(Value::ConstValueIterator p=prog.Begin();p!=prog.End();++p){
std:cout请提供一个完整的编译命令和您正在接收的错误消息。此外,如果代码没有编译,JSON的内容是无关的。那么jq
?cat f.JSON | jq.prog[]如何。inFiles[].pos.x1
感谢您的帮助。现在,该项目正在使用Visual Studio 2010进行编译,因此当我尝试上述操作时,会出现错误“无法推断'auto'类型(需要初始值设定项)”。您知道如何使用迭代器执行相同的操作吗?在我进行编辑之前,其他人已使用迭代器发布了相同的代码。您可以对此进行检查。