Important: Please read the 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 :

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

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

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

  • image_json.PNG

  • Lifetime Qt Champion


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

Log in to reply