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

Display the contents of a json file



  • Hello, I would like to display the contents of my json file in QML, can someone help me ? please

    here is the content of the json file :
    //test.json

    {
    "classe": "2btssn",
    "active": true,
    "nb": 3,
    "etudiants": [
    { "prénom": "John", "nom": "Doe", "age": 19 },
    { "prénom": "Jane", "nom": "Doe" , "age": 19 },
    { "prénom": "Robert", "nom": "Smith" , "age": 18 }
    ]
    }

    //here is the content of my main.cpp file

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QString>
    #include <QJsonDocument>
    #include <QFile>
    #include <QQmlContext>

    #include <QJsonObject>
    #include <QJsonArray>

    int main(int argc, char *argv[])
    {
    #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    #endif

    QGuiApplication app(argc, argv);
    
    QQmlApplicationEngine engine;
    QFile fichierJSON("test.json");
    if (fichierJSON.open(QFile::ReadOnly | QFile::Text))
    {
        // lecture les données du fichier JSON
        QString donnees = fichierJSON.readAll();
    
        fichierJSON.close();
    }
    
    
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    
    engine.load(url);
    
    return app.exec();
    

    }
    I want the content to display on the console but I can't
    thank you



  • image_json.PNG


  • Lifetime Qt Champion

    Hi,

    If it's only to dump on the application output using qDebug would be enough.


Log in to reply