Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Creating a JSON file



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

  • Moderators

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


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



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


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



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



  • @KroMignon thank you


Log in to reply