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. Creating a JSON file
QtWS25 Last Chance

Creating a JSON file

Scheduled Pinned Locked Moved Solved General and Desktop
jsonqt5write
7 Posts 4 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.
  • D Offline
    D Offline
    deleted286
    wrote on last edited by
    #1

    Hi everyone. I want to create my JSON file. I want to achieve a JSON file like this, I did not add the "Rota": at the beginning of json.

    {
        "Rota":
        {
            "Rota No":"4",
            "Baslangıc Nkt":"1",
            "Toplam Nkta":"12",
            "Degisim No":"7"
    
        },
        "Rota Nokta":
        {
            "enlem":"39.40",
            "boylam":"40.05",
            "irtifa":"120",
            "yaricap":"0.5",
            "no":"5",
            "sonraki":"10",
            "grv":"ileri"
        }
       }
    

    I've written the code below for it.

    #include <QFile>
    #include <QDebug>
    #include <QJsonParseError>
    #include <QJsonDocument>
    #include <QJsonObject>
    #include <QJsonArray>
    
    int main()
    {
        QFile file("/home/a/JsonWrite/rota.txt");
            if(!file.open(QIODevice::ReadWrite)) {
                qDebug() << "File open error";
            } else {
                qDebug() <<"File open!";
            }
    
         file.resize(0);
    
          QJsonArray jsonArray;
    
          QJsonObject rota;
    
          rota.insert("rota no", "5");
          rota.insert("baslangic nokta", "3");
          rota.insert("toplam nokta", "10");
         // rota.insert("degisim" "3");
    
    
         QJsonDocument doc;
         doc.setObject(rota);
         file.write(doc.toJson());
         file.close();
    
         qDebug() << "Write to file";
             return 0;
    
            
    }
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      "Rota" is part of parent JSON object. Something like:

      QJsonObject root;
      QJsonObject rota;
      rota.insert("rota no", "5");
      rota.insert("baslangic nokta", "3");
      rota.insert("toplam nokta", "10");
      root.insert("Rota", rota);
      QJsonDocument doc;
      doc.setObject(root);
      

      (Z(:^

      D 1 Reply Last reply
      2
      • sierdzioS sierdzio

        "Rota" is part of parent JSON object. Something like:

        QJsonObject root;
        QJsonObject rota;
        rota.insert("rota no", "5");
        rota.insert("baslangic nokta", "3");
        rota.insert("toplam nokta", "10");
        root.insert("Rota", rota);
        QJsonDocument doc;
        doc.setObject(root);
        
        D Offline
        D Offline
        deleted286
        wrote on last edited by deleted286
        #3

        @sierdzio I want to create another root object.
        For writing them in txt what should i do?
        Should I keep them an array?

        KroMignonK JonBJ 2 Replies Last reply
        0
        • D deleted286

          @sierdzio I want to create another root object.
          For writing them in txt what should i do?
          Should I keep them an array?

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          @suslucoder said in Creating a JSON file:

          I want to create another root object.

          AFAIK, JSON only have 1 root object, this is why it is called root.
          But the root object could be an array:

          QJsonArray root;
          
          QJsonObject rota;
          rota.insert("rota no", "5");
          rota.insert("baslangic nokta", "3");
          rota.insert("toplam nokta", "10");
          root.insert( rota);
          root.insert( rota);
          root.insert( rota);
          QJsonDocument doc;
          doc.setObject(root);
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          D 1 Reply Last reply
          4
          • D deleted286

            @sierdzio I want to create another root object.
            For writing them in txt what should i do?
            Should I keep them an array?

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

            @suslucoder

            I want to create another root object.

            Depends on what this new object is. If it's like the existing ones, do it the same way.

            For writing them in txt what should i do?

            Don't know what this means.

                QFile file("/home/a/JsonWrite/rota.txt");
                    if(!file.open(QIODevice::ReadWrite)) {
                        qDebug() << "File open error";
                    } else {
                        qDebug() <<"File open!";
                    }
            
                 file.resize(0);
            

            You're not reading from it, so don't open it read-write. You are resetting the size to 0. So just file.open(QIODevice::WriteOnly | QIODevice::Text) (JSON is text). And if you get an error, return from the function, don't keep going with a non-open file.

            D 1 Reply Last reply
            3
            • JonBJ JonB

              @suslucoder

              I want to create another root object.

              Depends on what this new object is. If it's like the existing ones, do it the same way.

              For writing them in txt what should i do?

              Don't know what this means.

                  QFile file("/home/a/JsonWrite/rota.txt");
                      if(!file.open(QIODevice::ReadWrite)) {
                          qDebug() << "File open error";
                      } else {
                          qDebug() <<"File open!";
                      }
              
                   file.resize(0);
              

              You're not reading from it, so don't open it read-write. You are resetting the size to 0. So just file.open(QIODevice::WriteOnly | QIODevice::Text) (JSON is text). And if you get an error, return from the function, don't keep going with a non-open file.

              D Offline
              D Offline
              deleted286
              wrote on last edited by
              #6

              @JonB said in Creating a JSON file:

              @suslucoder

              You're not reading from it, so don't open it read-write. You are resetting the size to 0. So just file.open(QIODevice::WriteOnly | QIODevice::Text) (JSON is text). And if you get an error, return from the function, don't keep going with a non-open file.

              I got it. Thank you.

              1 Reply Last reply
              1
              • KroMignonK KroMignon

                @suslucoder said in Creating a JSON file:

                I want to create another root object.

                AFAIK, JSON only have 1 root object, this is why it is called root.
                But the root object could be an array:

                QJsonArray root;
                
                QJsonObject rota;
                rota.insert("rota no", "5");
                rota.insert("baslangic nokta", "3");
                rota.insert("toplam nokta", "10");
                root.insert( rota);
                root.insert( rota);
                root.insert( rota);
                QJsonDocument doc;
                doc.setObject(root);
                
                D Offline
                D Offline
                deleted286
                wrote on last edited by
                #7

                @KroMignon thank you

                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