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 read and write values
Qt 6.11 is out! See what's new in the release blog

JSON read and write values

Scheduled Pinned Locked Moved Unsolved General and Desktop
29 Posts 6 Posters 5.6k Views 2 Watching
  • 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.
  • JonBJ JonB

    @another_one
    I don't know. suggest you put qDebug() 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 suitable QString method.

    jsulmJ Online
    jsulmJ Online
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #20

    @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...

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • JonBJ JonB

      @another_one
      I don't know. suggest you put qDebug() 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 suitable QString method.

      A Offline
      A Offline
      another_one
      wrote on last edited by
      #21

      @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

      jsulmJ 1 Reply Last reply
      0
      • A another_one

        @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

        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #22

        @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.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        A 1 Reply Last reply
        0
        • jsulmJ jsulm

          @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.

          A Offline
          A Offline
          another_one
          wrote on last edited by
          #23

          @jsulm
          Now I undestand, sorry, I need to do readall in QbyteArray as mzimmers already recomended
          Thanks to all!

          A 1 Reply Last reply
          0
          • A another_one

            @jsulm
            Now I undestand, sorry, I need to do readall in QbyteArray as mzimmers already recomended
            Thanks to all!

            A Offline
            A Offline
            another_one
            wrote on last edited by
            #24

            @another_one

            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";
            
                        }
            
            SGaistS 1 Reply Last reply
            0
            • A another_one

              @another_one

              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";
              
                          }
              
              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by SGaist
              #25

              @another_one said in JSON read and write values:

              @another_one

                          QJsonParseError qjpe3;
                          qba3 = file_json3.readAll();
                          file_json3.close();
                          QJsonDocument jsonResponse3 = QJsonDocument::fromJson(qba3, &qjpe3);
              

              What about checking what qjpe3 contains ?

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

              Christian EhrlicherC 1 Reply Last reply
              0
              • SGaistS SGaist

                @another_one said in JSON read and write values:

                @another_one

                            QJsonParseError qjpe3;
                            qba3 = file_json3.readAll();
                            file_json3.close();
                            QJsonDocument jsonResponse3 = QJsonDocument::fromJson(qba3, &qjpe3);
                

                What about checking what qjpe3 contains ?

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #26

                @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...

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                A 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @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...

                  A Offline
                  A Offline
                  another_one
                  wrote on last edited by
                  #27

                  @Christian-Ehrlicher

                  Thanks to all!, At last I found an error in on_open_folder_clicked()

                  JonBJ 1 Reply Last reply
                  0
                  • A another_one

                    @Christian-Ehrlicher

                    Thanks to all!, At last I found an error in on_open_folder_clicked()

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #28

                    @another_one And what was that error, given that you showed us the full source code of that function?

                    A 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @another_one And what was that error, given that you showed us the full source code of that function?

                      A Offline
                      A Offline
                      another_one
                      wrote on last edited by
                      #29

                      @JonB

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