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
Forum Update on Monday, May 27th 2025

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 28 Jan 2021, 08:55 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
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 28 Jan 2021, 09:04 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 28 Jan 2021, 09:10
      2
      • S sierdzio
        28 Jan 2021, 09:04

        "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 28 Jan 2021, 09:10 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?

        K J 2 Replies Last reply 28 Jan 2021, 09:20
        0
        • D deleted286
          28 Jan 2021, 09:10

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

          K Offline
          K Offline
          KroMignon
          wrote on 28 Jan 2021, 09:20 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 28 Jan 2021, 09:38
          4
          • D deleted286
            28 Jan 2021, 09:10

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

            J Offline
            J Offline
            JonB
            wrote on 28 Jan 2021, 09:21 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 28 Jan 2021, 09:37
            3
            • J JonB
              28 Jan 2021, 09:21

              @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 28 Jan 2021, 09:37 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
              • K KroMignon
                28 Jan 2021, 09:20

                @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 28 Jan 2021, 09:38 last edited by
                #7

                @KroMignon thank you

                1 Reply Last reply
                0

                4/7

                28 Jan 2021, 09:20

                • Login

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