I am trying to create a json file in which i insert QjsonObjects in an only one QJsonArray,what i get is every QjsonObject is in an independent QJsonArray but i want them to be in the same array.
this function is called every time a save button is clicked,and that's how my QJsonObjects are created.
void List::insertDefect(const QString &parentDefect,const QString &defect,const QString &positions)const{
QString filename =createListDefect();
QFile file(filename);
file.open(QIODevice::Append | QIODevice::Text);
QJsonObject defectObject;
defectObject.insert("parentDefect", QJsonValue::fromVariant(parentDefect));
defectObject.insert("defect", QJsonValue::fromVariant(defect));
defectObject.insert("positions", QJsonValue::fromVariant(positions));
QJsonArray listArray;
listArray.push_back(defectObject);
QJsonDocument doc(listArray);
file.write(doc.toJson(QJsonDocument::Indented));}
and here is an example of a generated json file :
[
{
"defect": "MISSING, DAMAGED",
"parentDefect": "SEAT BELTS",
"positions": "F | RB | "
}
]
[
{
"defect": "RIGIDITY,CORROSION,DISTORTION",
"parentDefect": "CHASSIS OR SUB-FRAME",
"positions": "B | RC | RB | "
}
]
and i am trying to make it look like this :
[
{
"defect": "MISSING, DAMAGED",
"parentDefect": "SEAT BELTS",
"positions": "F | RB | "
},
{
"defect": "RIGIDITY,CORROSION,DISTORTION",
"parentDefect": "CHASSIS OR SUB-FRAME",
"positions": "B | RC | RB | "
}
]
You are creating QJsonArray listArray;
as local variable inside the method, thus the array variable is destroyed after each call to the method and each object is being stored in separate new array, you have to create the array outside the method so that it persists over all calls, then add objects to it and update the document.
QJsonArray listArray;
void List::insertDefect()
....