我正在尝试创建一个 json 文件,在其中我将 QjsonObjects 插入只有一个 QJsonArray 中,我得到的是每个 QjsonObject 都在一个独立的 QJsonArray 中,但我希望它们位于同一个数组中。
每次单击保存按钮时都会调用此函数,这就是我的 QJsonObjects 的创建方式。
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));}
这是生成的 json 文件的示例:
[
{
"defect": "MISSING, DAMAGED",
"parentDefect": "SEAT BELTS",
"positions": "F | RB | "
}
]
[
{
"defect": "RIGIDITY,CORROSION,DISTORTION",
"parentDefect": "CHASSIS OR SUB-FRAME",
"positions": "B | RC | RB | "
}
]
我试图让它看起来像这样:
[
{
"defect": "MISSING, DAMAGED",
"parentDefect": "SEAT BELTS",
"positions": "F | RB | "
},
{
"defect": "RIGIDITY,CORROSION,DISTORTION",
"parentDefect": "CHASSIS OR SUB-FRAME",
"positions": "B | RC | RB | "
}
]