Json iteration
-
Hi
I am new to json with Qt and am having some difficulty traversing my Json file.{ "SEMCount": "2", "StringDef": { "SEMS": { "SEM": { "id": "0001", "ADCGain": "10" }, "SEM": { "id": "0002", "ADCGain": "20" } } } } I have taken this approach, but it only loops over the first 2 elements.
for (QJsonObject::const_iterator it = doc.Root().constBegin();
it != doc.Root().constEnd();
it++)
{
}``Any help is appreciated
-
Hi
I am new to json with Qt and am having some difficulty traversing my Json file.{ "SEMCount": "2", "StringDef": { "SEMS": { "SEM": { "id": "0001", "ADCGain": "10" }, "SEM": { "id": "0002", "ADCGain": "20" } } } } I have taken this approach, but it only loops over the first 2 elements.
for (QJsonObject::const_iterator it = doc.Root().constBegin();
it != doc.Root().constEnd();
it++)
{
}``Any help is appreciated
@RoachMan
Umm, well, there are indeed only 2 top-level elements in your document:SEMCount
andStringDef
. So it is doing exactly what it should do! What else did you expect? If you mean you want to descend the tree to visit every element then you are going to want to recurse on each element encountered. As you meet each QJsonValue you can examine its type via variousis...()
methods there and/or type() will return an enum QJsonValue::Type to tell you. If it's aQJsonValue::Object
(as it is for yourStringDef
) you need to recurse into it in the same way. -
@RoachMan
Umm, well, there are indeed only 2 top-level elements in your document:SEMCount
andStringDef
. So it is doing exactly what it should do! What else did you expect? If you mean you want to descend the tree to visit every element then you are going to want to recurse on each element encountered. As you meet each QJsonValue you can examine its type via variousis...()
methods there and/or type() will return an enum QJsonValue::Type to tell you. If it's aQJsonValue::Object
(as it is for yourStringDef
) you need to recurse into it in the same way.