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
Forum Updated to NodeBB v4.3 + New Features

JSON read and write values

Scheduled Pinned Locked Moved Unsolved General and Desktop
29 Posts 6 Posters 4.4k 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.
  • M mzimmers
    18 May 2023, 23:02

    @another_one note the final line in this code snippet:

        QByteArray qba = "{ \
                         \"PORT\": \"COM3\", \
                         \"RATE\": \"BAUD115200\", \
                         \"PARITY\": \"PAR_NONE\", \
                         \"STOPBIT\": \"STOP_2\", \
                         \"BITS\": \"8\", \
                         \"LD_ADDR\": \"FFFFFFF085000020\", \
                         \"ST_ADDR\": \"FFFFFFF085000020\", \
                         \"MEM_TYPE\": \"LWord\", \
                         \"MEM_ADDR\": \"FFFFFFF085000000\", \
                         \"FILE_LOAD\": \"C:/tmp\" \
                         }";
    
        QJsonDocument qjd = QJsonDocument::fromJson(qba);
        QJsonObject qjo = qjd.object();
        qjo["FILE_LOAD"] = "D:/tmp";
        qjd.setObject(qjo);
    
    J Offline
    J Offline
    JonB
    wrote on 19 May 2023, 06:31 last edited by JonB
    #5

    @mzimmers said in JSON read and write values:

    qjd.setObject(qjo);

    Quite right! I find the docs quite unobvious that this is necessary.

    1 Reply Last reply
    0
    • J JonB
      18 May 2023, 19:16

      @another_one
      Did you try

      jsonObject["FILE_LOAD"] = newValue;
      

      ?

      For saving back to file you have to save the whole document back.

      A Offline
      A Offline
      another_one
      wrote on 19 May 2023, 08:30 last edited by
      #6

      @JonB
      Thanks, I tried but I place new line instead of rewriting appropriate value:
      "
      {
      "PORT": "COM3",
      "RATE": "BAUD115200",
      "PARITY": "PAR_NONE",
      "STOPBIT": "STOP_2",
      "BITS": "8",
      "LD_ADDR": "FFFFFFFF08000020",
      "ST_ADDR": "FFFFFFFF08000020",
      "MEM_TYPE": "LWord",
      "MEM_ADDR": "FFFFFFF085000000",
      "FILE_LOAD": "C:/tmp",
      }

      {
      "FILE_LOAD": "C:/temp"
      }
      {
      "FILE_LOAD": "C:/temp"
      }
      {
      "FILE_LOAD": "C:/temp"
      }
      {
      "FILE_LOAD": "C:/temp"
      }
      {
      "FILE_LOAD": "C:/temp"
      }
      {
      "FILE_LOAD": "C:/temp"
      }
      {
      "FILE_LOAD": "C:/temp"
      }

      "

      J 1 Reply Last reply 19 May 2023, 08:54
      0
      • A another_one
        19 May 2023, 08:30

        @JonB
        Thanks, I tried but I place new line instead of rewriting appropriate value:
        "
        {
        "PORT": "COM3",
        "RATE": "BAUD115200",
        "PARITY": "PAR_NONE",
        "STOPBIT": "STOP_2",
        "BITS": "8",
        "LD_ADDR": "FFFFFFFF08000020",
        "ST_ADDR": "FFFFFFFF08000020",
        "MEM_TYPE": "LWord",
        "MEM_ADDR": "FFFFFFF085000000",
        "FILE_LOAD": "C:/tmp",
        }

        {
        "FILE_LOAD": "C:/temp"
        }
        {
        "FILE_LOAD": "C:/temp"
        }
        {
        "FILE_LOAD": "C:/temp"
        }
        {
        "FILE_LOAD": "C:/temp"
        }
        {
        "FILE_LOAD": "C:/temp"
        }
        {
        "FILE_LOAD": "C:/temp"
        }
        {
        "FILE_LOAD": "C:/temp"
        }

        "

        J Offline
        J Offline
        JonB
        wrote on 19 May 2023, 08:54 last edited by
        #7

        @another_one said in JSON read and write values:

        I tried but I place new line

        I don't know what this means. JSON does not have "new lines".

        You asked to "change g_FILE_LOAD and store it back". @mzimmers code is correct for updating the FILE_LOAD item to a new value, and then as I wrote "you have to save the whole document back", i.e. overwrite the existing file with the new QJsonDocument.

        1 Reply Last reply
        0
        • M mzimmers
          18 May 2023, 23:02

          @another_one note the final line in this code snippet:

              QByteArray qba = "{ \
                               \"PORT\": \"COM3\", \
                               \"RATE\": \"BAUD115200\", \
                               \"PARITY\": \"PAR_NONE\", \
                               \"STOPBIT\": \"STOP_2\", \
                               \"BITS\": \"8\", \
                               \"LD_ADDR\": \"FFFFFFF085000020\", \
                               \"ST_ADDR\": \"FFFFFFF085000020\", \
                               \"MEM_TYPE\": \"LWord\", \
                               \"MEM_ADDR\": \"FFFFFFF085000000\", \
                               \"FILE_LOAD\": \"C:/tmp\" \
                               }";
          
              QJsonDocument qjd = QJsonDocument::fromJson(qba);
              QJsonObject qjo = qjd.object();
              qjo["FILE_LOAD"] = "D:/tmp";
              qjd.setObject(qjo);
          
          A Offline
          A Offline
          another_one
          wrote on 19 May 2023, 09:10 last edited by
          #8

          @mzimmers

          is it necessary to repeat json context as "constant" in QByteArray, can I use already readed from json file?

          J 1 Reply Last reply 19 May 2023, 09:25
          0
          • A another_one
            19 May 2023, 09:10

            @mzimmers

            is it necessary to repeat json context as "constant" in QByteArray, can I use already readed from json file?

            J Offline
            J Offline
            JonB
            wrote on 19 May 2023, 09:25 last edited by
            #9

            @another_one Yes, of course you can read from file, @mzimmers was just a standalone example.

            A 1 Reply Last reply 19 May 2023, 09:59
            0
            • J JonB
              19 May 2023, 09:25

              @another_one Yes, of course you can read from file, @mzimmers was just a standalone example.

              A Offline
              A Offline
              another_one
              wrote on 19 May 2023, 09:59 last edited by
              #10

              @JonB
              Now I completely didnt understand)

              J 1 Reply Last reply 19 May 2023, 10:44
              0
              • A another_one
                19 May 2023, 09:59

                @JonB
                Now I completely didnt understand)

                J Offline
                J Offline
                JonB
                wrote on 19 May 2023, 10:44 last edited by
                #11

                @another_one
                I don't know what to say to that. Everything is really so straightforward.

                1. Open the file for read, read in the JSON, close the file.
                2. Make the change in memory to the FILE_LOAD value, as per @mzimmers code.
                3. Open the file for write/overwrite, save the JSON, close the file.
                A 1 Reply Last reply 19 May 2023, 11:29
                3
                • J JonB
                  19 May 2023, 10:44

                  @another_one
                  I don't know what to say to that. Everything is really so straightforward.

                  1. Open the file for read, read in the JSON, close the file.
                  2. Make the change in memory to the FILE_LOAD value, as per @mzimmers code.
                  3. Open the file for write/overwrite, save the JSON, close the file.
                  A Offline
                  A Offline
                  another_one
                  wrote on 19 May 2023, 11:29 last edited by
                  #12

                  @JonB
                  ok,
                  At first I have got the following values in json file:
                  "
                  {
                  "PORT": "COM3",
                  "RATE": "BAUD115200",
                  "PARITY": "PAR_NONE",
                  "STOPBIT": "STOP_2",
                  "BITS": "8",
                  "LD_ADDR": "FFFFFFF085000020",
                  "ST_ADDR": "FFFFFFF085000020",
                  "MEM_TYPE": "LWord",
                  "MEM_ADDR": "FFFFFFF085000000",
                  "FILE_LOAD": "C:/tmp",
                  }
                  "

                  And in code I do the followin:

                  1. open json file:
                   QFile file_json(QString::fromStdString("monMK.json"));
                      if (!file_json.open(QFile::WriteOnly | QFile::Text | QFile::Append)) {
                          qDebug() << "json didnt opened";
                  
                     }
                      else
                      {
                          qDebug() << "json oponed";
                          QString strReply = (QString)file_json.readAll();
                          QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
                  
                  1. Here I ment what my json file is already copied in memory (in jsonResponse), so I close the file:
                    file_json.close();
                  2. Now I do edition:
                   QJsonObject jsonObject = jsonResponse.object();
                   jsonObject["FILE_LOAD"] = "C:/temp";
                  jsonResponse.setObject(jsonObject);
                  
                  1. And now I store it back:
                    file_json.open(QFile::WriteOnly | QFile::Text | QFile::ReadWrite);
                    file_json.write(jsonResponse.toJson());
                  

                  As a result I have got the following jnson file context:
                  "
                  {
                  "FILE_LOAD": "C:/temp"
                  }
                  15200",
                  "PARITY": "PAR_NONE",
                  "STOPBIT": "STOP_2",
                  "BITS": "8",
                  "LD_ADDR": "FFFFFFF085000020",
                  "ST_ADDR": "FFFFFFF085000020",
                  "MEM_TYPE": "LWord",
                  "MEM_ADDR": "FFFFFFF085000000",
                  "FILE_LOAD": "C:/tmp",
                  }
                  "

                  J M 2 Replies Last reply 19 May 2023, 11:43
                  0
                  • A another_one
                    19 May 2023, 11:29

                    @JonB
                    ok,
                    At first I have got the following values in json file:
                    "
                    {
                    "PORT": "COM3",
                    "RATE": "BAUD115200",
                    "PARITY": "PAR_NONE",
                    "STOPBIT": "STOP_2",
                    "BITS": "8",
                    "LD_ADDR": "FFFFFFF085000020",
                    "ST_ADDR": "FFFFFFF085000020",
                    "MEM_TYPE": "LWord",
                    "MEM_ADDR": "FFFFFFF085000000",
                    "FILE_LOAD": "C:/tmp",
                    }
                    "

                    And in code I do the followin:

                    1. open json file:
                     QFile file_json(QString::fromStdString("monMK.json"));
                        if (!file_json.open(QFile::WriteOnly | QFile::Text | QFile::Append)) {
                            qDebug() << "json didnt opened";
                    
                       }
                        else
                        {
                            qDebug() << "json oponed";
                            QString strReply = (QString)file_json.readAll();
                            QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
                    
                    1. Here I ment what my json file is already copied in memory (in jsonResponse), so I close the file:
                      file_json.close();
                    2. Now I do edition:
                     QJsonObject jsonObject = jsonResponse.object();
                     jsonObject["FILE_LOAD"] = "C:/temp";
                    jsonResponse.setObject(jsonObject);
                    
                    1. And now I store it back:
                      file_json.open(QFile::WriteOnly | QFile::Text | QFile::ReadWrite);
                      file_json.write(jsonResponse.toJson());
                    

                    As a result I have got the following jnson file context:
                    "
                    {
                    "FILE_LOAD": "C:/temp"
                    }
                    15200",
                    "PARITY": "PAR_NONE",
                    "STOPBIT": "STOP_2",
                    "BITS": "8",
                    "LD_ADDR": "FFFFFFF085000020",
                    "ST_ADDR": "FFFFFFF085000020",
                    "MEM_TYPE": "LWord",
                    "MEM_ADDR": "FFFFFFF085000000",
                    "FILE_LOAD": "C:/tmp",
                    }
                    "

                    J Offline
                    J Offline
                    JonB
                    wrote on 19 May 2023, 11:43 last edited by JonB
                    #13

                    @another_one
                    This is getting crazy. You already had code which read in your JSON from file. Now you open a file for WriteOnly + Append initially, even though we said to open it for read, and then you try to read from it.

                    Further in the second stage you open a file for WriteOnly + ReadWrite when we said to overwrite.

                    1 Reply Last reply
                    1
                    • A another_one
                      19 May 2023, 11:29

                      @JonB
                      ok,
                      At first I have got the following values in json file:
                      "
                      {
                      "PORT": "COM3",
                      "RATE": "BAUD115200",
                      "PARITY": "PAR_NONE",
                      "STOPBIT": "STOP_2",
                      "BITS": "8",
                      "LD_ADDR": "FFFFFFF085000020",
                      "ST_ADDR": "FFFFFFF085000020",
                      "MEM_TYPE": "LWord",
                      "MEM_ADDR": "FFFFFFF085000000",
                      "FILE_LOAD": "C:/tmp",
                      }
                      "

                      And in code I do the followin:

                      1. open json file:
                       QFile file_json(QString::fromStdString("monMK.json"));
                          if (!file_json.open(QFile::WriteOnly | QFile::Text | QFile::Append)) {
                              qDebug() << "json didnt opened";
                      
                         }
                          else
                          {
                              qDebug() << "json oponed";
                              QString strReply = (QString)file_json.readAll();
                              QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
                      
                      1. Here I ment what my json file is already copied in memory (in jsonResponse), so I close the file:
                        file_json.close();
                      2. Now I do edition:
                       QJsonObject jsonObject = jsonResponse.object();
                       jsonObject["FILE_LOAD"] = "C:/temp";
                      jsonResponse.setObject(jsonObject);
                      
                      1. And now I store it back:
                        file_json.open(QFile::WriteOnly | QFile::Text | QFile::ReadWrite);
                        file_json.write(jsonResponse.toJson());
                      

                      As a result I have got the following jnson file context:
                      "
                      {
                      "FILE_LOAD": "C:/temp"
                      }
                      15200",
                      "PARITY": "PAR_NONE",
                      "STOPBIT": "STOP_2",
                      "BITS": "8",
                      "LD_ADDR": "FFFFFFF085000020",
                      "ST_ADDR": "FFFFFFF085000020",
                      "MEM_TYPE": "LWord",
                      "MEM_ADDR": "FFFFFFF085000000",
                      "FILE_LOAD": "C:/tmp",
                      }
                      "

                      M Offline
                      M Offline
                      mzimmers
                      wrote on 19 May 2023, 13:44 last edited by
                      #14

                      @another_one your JSON logic now looks OK, but your file handling is wrong.

                          QFile qf("./test.json");
                          QByteArray qba;
                          QJsonParseError qjpe;
                          QJsonDocument qjd;
                      
                          qf.open(QFile::ReadOnly);
                          qba = qf.readAll();
                          qf.close();
                          qjd = QJsonDocument::fromJson(qba, &qjpe);
                          QJsonObject qjo = qjd.object();
                          qjo["FILE_LOAD"] = "D:/tmp";
                          qjd.setObject(qjo);
                      
                          qf.open(QFile::WriteOnly);
                          qba = qjd.toJson();
                          qf.write(qba);
                          qf.close();
                      
                      
                      S 1 Reply Last reply 19 May 2023, 13:50
                      2
                      • M mzimmers
                        19 May 2023, 13:44

                        @another_one your JSON logic now looks OK, but your file handling is wrong.

                            QFile qf("./test.json");
                            QByteArray qba;
                            QJsonParseError qjpe;
                            QJsonDocument qjd;
                        
                            qf.open(QFile::ReadOnly);
                            qba = qf.readAll();
                            qf.close();
                            qjd = QJsonDocument::fromJson(qba, &qjpe);
                            QJsonObject qjo = qjd.object();
                            qjo["FILE_LOAD"] = "D:/tmp";
                            qjd.setObject(qjo);
                        
                            qf.open(QFile::WriteOnly);
                            qba = qjd.toJson();
                            qf.write(qba);
                            qf.close();
                        
                        
                        S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 19 May 2023, 13:50 last edited by
                        #15

                        In addition to what @mzimmers wrote, stop your C style casting. This is very wrong. QByteArray and QString are two very different beasts.

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

                        A 1 Reply Last reply 23 May 2023, 17:28
                        0
                        • S SGaist
                          19 May 2023, 13:50

                          In addition to what @mzimmers wrote, stop your C style casting. This is very wrong. QByteArray and QString are two very different beasts.

                          A Offline
                          A Offline
                          another_one
                          wrote on 23 May 2023, 17:28 last edited by
                          #16

                          @SGaist Thank you!

                          almost everything worked
                          At the setup ui I write values to JSON file
                          But when I trying to write during filedialog launched by button clicking its not working

                           QFile file_json2(QString::fromStdString("./monMK.json"));
                                      if (!file_json2.open(QFile::ReadOnly))
                                      {
                                          qDebug() << "json didnt opened";
                                      }
                                      else
                                      {
                                          qDebug() << "json oponed";
                                          QString strReply2 = (QString)file_json2.readAll();
                                          qDebug() << strReply2;
                          
                                          file_json2.close();
                                          QJsonDocument jsonResponse2 = QJsonDocument::fromJson(strReply2.toUtf8());
                                          QJsonObject jsonObject2 = jsonResponse2.object();
                          
                                          jsonObject2["FILE_LOAD"] = g_FILE_LOAD;
                                          jsonResponse2.setObject(jsonObject2);
                                          file_json2.open(QFile::WriteOnly);
                          
                                          file_json2.write(jsonResponse2.toJson());
                                          qDebug() << "g_FILE_LOAD" << g_FILE_LOAD;
                                          file_json2.close();
                                          qDebug() << strReply2;
                          
                                      }
                          

                          As I understand its not possible cause of another processing
                          and I should do JSON file storing by slot function emitted from filedialog of button processing
                          But I don't undestand how to do an object which has to be as emitter of signal

                          Please help!

                          J 1 Reply Last reply 23 May 2023, 18:39
                          0
                          • A another_one
                            23 May 2023, 17:28

                            @SGaist Thank you!

                            almost everything worked
                            At the setup ui I write values to JSON file
                            But when I trying to write during filedialog launched by button clicking its not working

                             QFile file_json2(QString::fromStdString("./monMK.json"));
                                        if (!file_json2.open(QFile::ReadOnly))
                                        {
                                            qDebug() << "json didnt opened";
                                        }
                                        else
                                        {
                                            qDebug() << "json oponed";
                                            QString strReply2 = (QString)file_json2.readAll();
                                            qDebug() << strReply2;
                            
                                            file_json2.close();
                                            QJsonDocument jsonResponse2 = QJsonDocument::fromJson(strReply2.toUtf8());
                                            QJsonObject jsonObject2 = jsonResponse2.object();
                            
                                            jsonObject2["FILE_LOAD"] = g_FILE_LOAD;
                                            jsonResponse2.setObject(jsonObject2);
                                            file_json2.open(QFile::WriteOnly);
                            
                                            file_json2.write(jsonResponse2.toJson());
                                            qDebug() << "g_FILE_LOAD" << g_FILE_LOAD;
                                            file_json2.close();
                                            qDebug() << strReply2;
                            
                                        }
                            

                            As I understand its not possible cause of another processing
                            and I should do JSON file storing by slot function emitted from filedialog of button processing
                            But I don't undestand how to do an object which has to be as emitter of signal

                            Please help!

                            J Offline
                            J Offline
                            JonB
                            wrote on 23 May 2023, 18:39 last edited by JonB
                            #17

                            @another_one
                            None of the "file dialog" or "slot" or "signal" stuff you mention should be of any relevance. Your code looks like it should work OK. Why don't you say something descriptive rather than "its not working" if you're a programmer and you want help?

                            A 1 Reply Last reply 24 May 2023, 18:10
                            0
                            • J JonB
                              23 May 2023, 18:39

                              @another_one
                              None of the "file dialog" or "slot" or "signal" stuff you mention should be of any relevance. Your code looks like it should work OK. Why don't you say something descriptive rather than "its not working" if you're a programmer and you want help?

                              A Offline
                              A Offline
                              another_one
                              wrote on 24 May 2023, 18:10 last edited by
                              #18

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

                              So please help to figure out

                              Thanks!

                              J 1 Reply Last reply 24 May 2023, 20:38
                              0
                              • A another_one
                                24 May 2023, 18:10

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

                                So please help to figure out

                                Thanks!

                                J Offline
                                J Offline
                                JonB
                                wrote on 24 May 2023, 20:38 last edited by JonB
                                #19

                                @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 A 2 Replies Last reply 25 May 2023, 06:02
                                0
                                • J JonB
                                  24 May 2023, 20:38

                                  @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 Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 25 May 2023, 06:02 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
                                  • J JonB
                                    24 May 2023, 20:38

                                    @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 25 May 2023, 08:25 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 25 May 2023, 10:07
                                    0
                                    • A another_one
                                      25 May 2023, 08:25

                                      @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 Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on 25 May 2023, 10:07 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 25 May 2023, 11:06
                                      0
                                      • jsulmJ jsulm
                                        25 May 2023, 10:07

                                        @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 25 May 2023, 11:06 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 25 May 2023, 12:06
                                        0
                                        • A another_one
                                          25 May 2023, 11:06

                                          @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 25 May 2023, 12:06 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";
                                          
                                                      }
                                          
                                          S 1 Reply Last reply 25 May 2023, 19:12
                                          0

                                          14/29

                                          19 May 2023, 13:44

                                          • Login

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