gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:58:08 25 4
gpt4 key购买 nike

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

}

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

最佳答案

这是最终的工作

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

关于c++ - Rapidjson 迭代并获取复杂 JSON 对象成员的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51796132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com