Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Json import [Solved]

Json import [Solved]

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GreenFire
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      GreenFire
      wrote on last edited by
      #2

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

      }-* @@-@

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GreenFire
        wrote on last edited by
        #3

        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"
        }
        }@

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • G Offline
            G Offline
            GreenFire
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              What about:

              @QJsonObject tem2d = input["TEM 2D"].toObject()@

              And parse this one ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • G Offline
                G Offline
                GreenFire
                wrote on last edited by
                #7

                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);@
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Nice !

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved