JSON read and write values
-
@JonB
Thank you for your replay!I'm trying to strore the file path in JSON value "FILE_LOAD" in processing of selecting *.bin file in windows folder
To do what I click on "open_folder" button below:void M_m::on_open_folder_clicked() { QDir dir2; dir2.setCurrent(g_FILE_LOAD); QString nameFile2 = QFileDialog::getOpenFileName(this, tr("load bin"),"", tr("*.bin")); if (nameFile2.isEmpty()) { return; } else { QFile file; file.setFileName(nameFile2); ui->lineEdit->setStyleSheet("color: green; background-color: white"); ui->lineEdit->setText(nameFile2); QFileInfo nameSt(file); QString name = nameSt.fileName(); qDebug()<<"objectNAme="<<file.objectName();//.fileName(); dir2 = nameSt.dir();//.absoluteDir(); g_FILE_LOAD = nameFile2; qDebug() << QString::number((unsigned int)fls, 16); fs2->setValue(3); } }
where fs2->setValue(3); launch Slot function in which I do the following:
QFile file_json3(QString::fromStdString("monMK.json")); if (!file_json3.open(QFile::ReadOnly)) { qDebug() << "json didnt opened"; } else { qDebug() << "json value3 opened"; QString strReply3 = (QString)file_json3.readAll(); qDebug() << strReply3; // read JSON in debug QJsonDocument jsonResponse3 = QJsonDocument::fromJson(strReply3.toUtf8()); QJsonObject jsonObject3 = jsonResponse3.object(); file_json3.close(); jsonObject3["FILE_LOAD"] =g_FILE_LOAD; jsonResponse3.setObject(jsonObject3); file_json3.open(QFile::WriteOnly); file_json3.write(jsonResponse3.toJson()); qDebug() << "g_FILE_LOAD" << "C:/temp"; // g_FILE_LOAD; file_json3.close(); qDebug() << strReply3; qDebug() << "store finished"; }
And only in what case my writing to JSON file is not working
Also I do the almost the same - I store another value from another button clicking in which I use more simplier processing whithout file dialog and it is working
And I compared whese two cases and as result I got the following:
In more simplier case I read JSON in debug and that is correct:
"
json value 1 oponed
QJsonValue(string, "FFFFFFF085000020")
"{\n "BITS": "8",\n "FILE_COMP": "Undefined",\n "FILE_LOAD": "C:/temp",\n "FLOAD": "C:/temp",\n "LD_ADDR": "FFFFFFF085000020",\n "MEM_ADDR": "FFFFFFF085000000",\n "MEM_TYPE": "LWord",\n "PARITY": "PAR_NONE",\n "PORT": "COM3",\n "RATE": "115200",\n "STOPBIT": "STOP_2",\n "ST_ADDR": "FFFFFFF085000000"\n}\n"
"
In case with problem of storing I read JSON in debug and that is not correct:
"
json value3 oponed
"{\n "FILE_LOAD": "C:/tmp",\n "PORT": "PORT20"\n}\n"
g_FILE_LOAD C:/temp
"{\n "FILE_LOAD": "C:/tmp",\n "PORT": "PORT20"\n}\n"
"
And maybe because of what I can not do right writingSo please help to figure out
Thanks!
@another_one
I don't know. suggest you putqDebug()
statements after each line as you change the JSON to see what's going on where. The fact that you use a dialog to get a pathname from the user cannot be relevant.QString strReply3 = (QString)file_json3.readAll();
do not use this. Use a suitableQString
method. -
@another_one
I don't know. suggest you putqDebug()
statements after each line as you change the JSON to see what's going on where. The fact that you use a dialog to get a pathname from the user cannot be relevant.QString strReply3 = (QString)file_json3.readAll();
do not use this. Use a suitableQString
method.@JonB said in JSON read and write values:
do not use this. Use a suitable QString method.
@another_one @SGaist wrote that several times already, don't know why you're not lisening...
-
@another_one
I don't know. suggest you putqDebug()
statements after each line as you change the JSON to see what's going on where. The fact that you use a dialog to get a pathname from the user cannot be relevant.QString strReply3 = (QString)file_json3.readAll();
do not use this. Use a suitableQString
method.@JonB said in JSON read and write values:
Use a suitable QString method.
Sorry I didint undestand, what is why I still do not use what
Could you bring some example?, please -
@JonB said in JSON read and write values:
Use a suitable QString method.
Sorry I didint undestand, what is why I still do not use what
Could you bring some example?, please@another_one You can't use C-style cast to cat a QByteArray to QString.
Use one of the from* methods like https://doc.qt.io/qt-6/qstring.html#fromLatin1-2 depending on the used encoding. -
@another_one You can't use C-style cast to cat a QByteArray to QString.
Use one of the from* methods like https://doc.qt.io/qt-6/qstring.html#fromLatin1-2 depending on the used encoding.@jsulm
Now I undestand, sorry, I need to do readall in QbyteArray as mzimmers already recomended
Thanks to all! -
@jsulm
Now I undestand, sorry, I need to do readall in QbyteArray as mzimmers already recomended
Thanks to all!I did recomended changes but unfortunally it doesn help!
QFile file_json3(QString::fromStdString("monMK.json")); if (!file_json3.open(QFile::ReadOnly)) { qDebug() << "json didnt opened"; } else { QByteArray qba3; QJsonParseError qjpe3; qba3 = file_json3.readAll(); file_json3.close(); QJsonDocument jsonResponse3 = QJsonDocument::fromJson(qba3, &qjpe3); QJsonObject jsonObject3 = jsonResponse3.object(); jsonObject3["FILE_LOAD"] = g_FILE_LOAD; jsonResponse3.setObject(jsonObject3); file_json3.open(QFile::WriteOnly); qba3 = jsonResponse3.toJson(); file_json3.write(qba3); file_json3.close(); qDebug() << qba3; qDebug() << "store finished"; }
-
I did recomended changes but unfortunally it doesn help!
QFile file_json3(QString::fromStdString("monMK.json")); if (!file_json3.open(QFile::ReadOnly)) { qDebug() << "json didnt opened"; } else { QByteArray qba3; QJsonParseError qjpe3; qba3 = file_json3.readAll(); file_json3.close(); QJsonDocument jsonResponse3 = QJsonDocument::fromJson(qba3, &qjpe3); QJsonObject jsonObject3 = jsonResponse3.object(); jsonObject3["FILE_LOAD"] = g_FILE_LOAD; jsonResponse3.setObject(jsonObject3); file_json3.open(QFile::WriteOnly); qba3 = jsonResponse3.toJson(); file_json3.write(qba3); file_json3.close(); qDebug() << qba3; qDebug() << "store finished"; }
@another_one said in JSON read and write values:
QJsonParseError qjpe3; qba3 = file_json3.readAll(); file_json3.close(); QJsonDocument jsonResponse3 = QJsonDocument::fromJson(qba3, &qjpe3);
What about checking what qjpe3 contains ?
-
@another_one said in JSON read and write values:
QJsonParseError qjpe3; qba3 = file_json3.readAll(); file_json3.close(); QJsonDocument jsonResponse3 = QJsonDocument::fromJson(qba3, &qjpe3);
What about checking what qjpe3 contains ?
@SGaist said in JSON read and write values:
Why about checking what qjpe3 contains ?
And on the next line don't ignore
qjpe3
. Doing basically no error checking and the onwdering why it does not work... -
@SGaist said in JSON read and write values:
Why about checking what qjpe3 contains ?
And on the next line don't ignore
qjpe3
. Doing basically no error checking and the onwdering why it does not work...Thanks to all!, At last I found an error in on_open_folder_clicked()
-
Thanks to all!, At last I found an error in on_open_folder_clicked()
@another_one And what was that error, given that you showed us the full source code of that function?
-
@another_one And what was that error, given that you showed us the full source code of that function?
Hello!
I find what reason by elimination method
And on_open_folder_clicked() become the following:void M_m::on_open_folder_clicked() { QFileDialog dialog(this); dialog.setFileMode(QFileDialog::Directory); QString nameFile2 = dialog.getOpenFileName(this, tr("load bin"),"", tr("*.bin")); qDebug() << "file name at first "<< nameFile2; if (nameFile2.isEmpty()) { return; } else { qDebug() << "dirdir2"; ui->lineEdit->setStyleSheet("color: green; background-color: white"); ui->lineEdit->setText(nameFile2); g_FILE_LOAD = nameFile2; qDebug() << QString::number((unsigned int)fls, 16); ui->lineEdit->setStyleSheet("color: green; background-color: white"); ui->lineEdit->setText(nameFile2); qDebug() << "new g_FILE_LOAD" << g_FILE_LOAD; } fs2->setValue(3); }