Json import [Solved]



  • I am importing from a QJsonDocument and it was working until i gave it a header and it stopped here is the code any suggestions



  • @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);
    

    }-* @@-@



  • the file i am trying to import looks like this

    @{
    "TEM 2D": {
    "Acquisition Software": "Xplore 3D",
    "Camera": "AMT",
    "Cs Value": "30",
    "Energy Filter Slit Size": "30",
    "Voltage": "30"
    }
    }@


  • Lifetime Qt Champion

    Hi,

    Are you sure that QJsonDocument::fromJson is successful ? Did you try to see what the error parameter returns ?



  • so if the Acquisition software and all the others are at the level of TEM 2D it will import but after i added the TEM 2D it will not and i am stumpped


  • Lifetime Qt Champion

    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);@

  • Lifetime Qt Champion

    Nice !

    Don't forget to update the thread's title by prepending [solved] so other forum users will know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.