PDA

View Full Version : Data gets overwritten in Json file



shoeb
5th April 2016, 11:59
Hi,

I'm not able to append mutliple data to Json file. My code is as;



void MainWindow::fileWriteOperationJson()
{
QString filename = "C:/Users/..../save.json";

QFile saveFile(filename);
saveFile.open(QIODevice::WriteOnly|QIODevice::Text );

// if (!saveFile.open(QIODevice::WriteOnly))
// {
// qWarning("Couldn't open save file.");
// }
QJsonObject json;
write(json);
QTextStream out(&saveFile);

out << QJsonDocument(json).toJson(QJsonDocument::Indented );

}



void MainWindow::write(QJsonObject &json) const
{
QJsonObject nameObj;
nameObj["f_name"] = ui->lineEdit->text();
nameObj["l_name"] = ui->lineEdit_2->text();

QJsonArray personalData;
personalData.append(nameObj);
json["personal_Data"] = personalData;

qDebug() << qlist;
}


My current output is as:


{
"personal_Data": [
{
"f_name": "qqq",
"l_name": "www"
}
]
}


Please suggest how should i append the multiple info??

Thanks in advance :)

-Shoeb

ChrisW67
6th April 2016, 19:57
if you want a list of JSON objects representing people then you should use a QJsonArray (list) of QJSonObject (people).