Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Display the contents of a json file
Forum Updated to NodeBB v4.3 + New Features

Display the contents of a json file

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 267 Views 1 Watching
  • 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.
  • O Offline
    O Offline
    Oussou
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • O Offline
      O Offline
      Oussou
      wrote on last edited by
      #2

      image_json.PNG

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        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