Json import [Solved]
-
@void Tem2D::on_Import_clicked()
{QString inFile; QFile file; QString filename = QFileDialog::getOpenFileName(this, "Open file", "" ,""); file.setFileName(filename); file.open(QIODevice::ReadWrite | QIODevice::Text); inFile = file.readAll(); file.close(); QJsonDocument input = QJsonDocument::fromJson(inFile.toUtf8()); sessionModel = input.object(); QString csval = QJsonValue(sessionModel.value("Cs Value")).toString(); ui->csValue_Text->setText(csval); QString voltageVal = QJsonValue(sessionModel.value("Voltage")).toString(); ui->voltage_Text->setText(voltageVal); QString efssVal = QJsonValue(sessionModel.value("Energy Filter Slit Size")).toString(); ui->energyFilterSlitSize_Text->setText(efssVal); QString camVal = QJsonValue(sessionModel.value("Camera")).toString(); ui->camera->setCurrentText(camVal); QString acqSofVal = QJsonValue(sessionModel.value("Acquisition Software")).toString(); ui->acquistionSoftware->setCurrentText(acqSofVal);
}-* @@-@
-
Hi,
Are you sure that QJsonDocument::fromJson is successful ? Did you try to see what the error parameter returns ?
-
What about:
@QJsonObject tem2d = input["TEM 2D"].toObject()@
And parse this one ?
-
This Code is what works
@ QString inFile;
QFile file;
QString filename = QFileDialog::getOpenFileName(this, "Open file", "" ,"");
file.setFileName(filename);
file.open(QIODevice::ReadWrite | QIODevice::Text);
inFile = file.readAll();
file.close();QJsonDocument input = QJsonDocument::fromJson(inFile.toUtf8()); sessionModel = input.object(); QJsonObject tem2D = QJsonValue(sessionModel.value("TEM 2D")).toObject(); QString csval = QJsonValue(tem2D.value("Cs Value")).toString(); ui->csValue_Text->setText(csval); QString voltageVal = QJsonValue(tem2D.value("Voltage")).toString(); ui->voltage_Text->setText(voltageVal); QString efssVal = QJsonValue(tem2D.value("Energy Filter Slit Size")).toString(); ui->energyFilterSlitSize_Text->setText(efssVal); QString camVal = QJsonValue(tem2D.value("Camera")).toString(); ui->camera->setCurrentText(camVal); QString acqSofVal = QJsonValue(tem2D.value("Acquisition Software")).toString(); ui->acquistionSoftware->setCurrentText(acqSofVal);@
-
Nice !
Don't forget to update the thread's title by prepending [solved] so other forum users will know a solution has been found :)