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. How should i store the results ??
QtWS25 Last Chance

How should i store the results ??

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcreatorjson parserjsonqbytearrayqt5
6 Posts 2 Posters 1.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.
  • Q Offline
    Q Offline
    Qjay
    wrote on 26 May 2018, 12:50 last edited by
    #1

    Hello i have a simple Qprocess program which calls meson and returns results in JSON .
    [{"name": "myexe", "installed": false, "id": "myexe@exe", "filename": "myexe", "type": "executable", "build_by_default": true}]

    QCoreApplication a(argc, argv);
        QObject *parent = nullptr;
        QProcess *process = new QProcess(parent);
        const QString program  = "/usr/local/bin/meson";
        const QStringList args = {"introspect", "--targets", "/home/user/meson/builddir/"};
      
        process->start(program, args);
        int res = process->waitForFinished();
    
        if ( !res ) {
            qDebug() << process->errorString();
    
        }
        process->waitForFinished();
        QByteArray output = process->readAllStandardOutput();
        qDebug() << "output : " << output << "\n";
    

    It returns a QByteArray , how can i save it in json or something similar where i can extract each element

    i tried like this :

    JsonDocument item = QJsonDocument::fromJson(output);
        qDebug() << "item : " << item << "\n";
        QJsonArray rootobj = item.array();
        qDebug() << "rootobj : " << rootobj << "\n";
    
    

    but i am not able to access individual element .

    P 1 Reply Last reply 26 May 2018, 14:48
    0
    • Q Qjay
      26 May 2018, 12:50

      Hello i have a simple Qprocess program which calls meson and returns results in JSON .
      [{"name": "myexe", "installed": false, "id": "myexe@exe", "filename": "myexe", "type": "executable", "build_by_default": true}]

      QCoreApplication a(argc, argv);
          QObject *parent = nullptr;
          QProcess *process = new QProcess(parent);
          const QString program  = "/usr/local/bin/meson";
          const QStringList args = {"introspect", "--targets", "/home/user/meson/builddir/"};
        
          process->start(program, args);
          int res = process->waitForFinished();
      
          if ( !res ) {
              qDebug() << process->errorString();
      
          }
          process->waitForFinished();
          QByteArray output = process->readAllStandardOutput();
          qDebug() << "output : " << output << "\n";
      

      It returns a QByteArray , how can i save it in json or something similar where i can extract each element

      i tried like this :

      JsonDocument item = QJsonDocument::fromJson(output);
          qDebug() << "item : " << item << "\n";
          QJsonArray rootobj = item.array();
          qDebug() << "rootobj : " << rootobj << "\n";
      
      

      but i am not able to access individual element .

      P Offline
      P Offline
      Paul Colby
      wrote on 26 May 2018, 14:48 last edited by Paul Colby
      #2

      Hi @Qjay,

      but i am not able to access individual element .

      Here's a basic example:

      const QByteArray output("[{\"name\": \"myexe\", \"installed\": false, \"id\": \"myexe@exe\", \"filename\": \"myexe\", \"type\": \"executable\", \"build_by_default\": true}]");
      qDebug().noquote() << output;
      
      const QJsonDocument doc = QJsonDocument::fromJson(output);
      qDebug().noquote() << doc;
      qDebug().noquote() << doc.array();
      qDebug().noquote() << doc.array().first();
      qDebug().noquote() << doc.array().first().toObject();
      qDebug().noquote() << doc.array().first().toObject().value("name");
      qDebug().noquote() << doc.array().first().toObject().value("name").toString();
      qDebug().noquote() << doc.array().first().toObject().value("installed");
      qDebug().noquote() << doc.array().first().toObject().value("installed").toBool();
      

      The output:

      [{"name": "myexe", "installed": false, "id": "myexe@exe", "filename": "myexe", "type": "executable", "build_by_default": true}]
      QJsonDocument([{"build_by_default":true,"filename":"myexe","id":"myexe@exe","installed":false,"name":"myexe","type":"executable"}])
      QJsonArray([{"build_by_default":true,"filename":"myexe","id":"myexe@exe","installed":false,"name":"myexe","type":"executable"}])
      QJsonValue(object, QJsonObject({"build_by_default":true,"filename":"myexe","id":"myexe@exe","installed":false,"name":"myexe","type":"executable"}))
      QJsonObject({"build_by_default":true,"filename":"myexe","id":"myexe@exe","installed":false,"name":"myexe","type":"executable"})
      QJsonValue(string, myexe)
      myexe
      QJsonValue(bool, false)
      false
      

      Above, I used .first() since the JSON array only has one item, but if you expected any number of items, you can loop through it like:

      const QJsonDocument doc = QJsonDocument::fromJson(output);
      qDebug().noquote() << doc;
      qDebug().noquote() << doc.array();
      foreach (auto value, doc.array()) {
          qDebug().noquote() << value;
          qDebug().noquote() << value.toObject();
          qDebug().noquote() << value.toObject().value("name");
          qDebug().noquote() << value.toObject().value("name").toString();
          qDebug().noquote() << value.toObject().value("installed");
          qDebug().noquote() << value.toObject().value("installed").toBool();
      }
      

      (same output)

      Cheers.

      1 Reply Last reply
      6
      • Q Offline
        Q Offline
        Qjay
        wrote on 26 May 2018, 16:30 last edited by
        #3

        @Paul-Colby said in How should i store the results ??:

        qDebug().noquote() << doc;
        qDebug().noquote() << doc.array();
        foreach (auto value, doc.array()) {
        qDebug().noquote() << value;
        qDebug().noquote() << value.toObject();
        qDebug().noquote() << value.toObject().value("name");
        qDebug().noquote() << value.toObject().value("name").toString();
        qDebug().noquote() << value.toObject().value("installed");
        qDebug().noquote() << value.toObject().value("installed").toBool();
        }

        Thanks , exactly what i was looking for :)

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          Qjay
          wrote on 31 May 2018, 06:05 last edited by
          #4

          @Paul-Colby how can i store the results as QVariantMap ?

          P 1 Reply Last reply 31 May 2018, 06:35
          0
          • Q Qjay
            31 May 2018, 06:05

            @Paul-Colby how can i store the results as QVariantMap ?

            P Offline
            P Offline
            Paul Colby
            wrote on 31 May 2018, 06:35 last edited by
            #5

            Hi @Qjay,

            @Paul-Colby how can i store the results as QVariantMap ?

            Since the document is an array, you should convert it to a QVariantList rather than QVariantMap, otherwise, you can convert individual entries to QVariantMap objects. Here's a quick example of both:

            const QByteArray output("[{\"name\": \"myexe\", \"installed\": false, \"id\": \"myexe@exe\", \"filename\": \"myexe\", \"type\": \"executable\", \"build_by_default\": true}]");
            qDebug().noquote() << "bytes" << output;
            
            const QJsonDocument doc = QJsonDocument::fromJson(output);
            qDebug().noquote() << "doc:" << doc;
            qDebug().noquote() << "list:" << doc.array().toVariantList();
            foreach (auto value, doc.array()) {
                qDebug() << "map:" << value.toObject().toVariantMap();
            }
            

            Output:

            bytes [{"name": "myexe", "installed": false, "id": "myexe@exe", "filename": "myexe", "type": "executable", "build_by_default": true}]
            doc: QJsonDocument([{"build_by_default":true,"filename":"myexe","id":"myexe@exe","installed":false,"name":"myexe","type":"executable"}])
            list: (QVariant(QVariantMap, QMap((build_by_default, QVariant(bool, true))(filename, QVariant(QString, myexe))(id, QVariant(QString, myexe@exe))(installed, QVariant(bool, false))(name, QVariant(QString, myexe))(type, QVariant(QString, executable)))))
            map: QMap(("build_by_default", QVariant(bool, true))("filename", QVariant(QString, "myexe"))("id", QVariant(QString, "myexe@exe"))("installed", QVariant(bool, false))("name", QVariant(QString, "myexe"))("type", QVariant(QString, "executable")))
            

            Cheers.

            Q 1 Reply Last reply 31 May 2018, 12:26
            4
            • P Paul Colby
              31 May 2018, 06:35

              Hi @Qjay,

              @Paul-Colby how can i store the results as QVariantMap ?

              Since the document is an array, you should convert it to a QVariantList rather than QVariantMap, otherwise, you can convert individual entries to QVariantMap objects. Here's a quick example of both:

              const QByteArray output("[{\"name\": \"myexe\", \"installed\": false, \"id\": \"myexe@exe\", \"filename\": \"myexe\", \"type\": \"executable\", \"build_by_default\": true}]");
              qDebug().noquote() << "bytes" << output;
              
              const QJsonDocument doc = QJsonDocument::fromJson(output);
              qDebug().noquote() << "doc:" << doc;
              qDebug().noquote() << "list:" << doc.array().toVariantList();
              foreach (auto value, doc.array()) {
                  qDebug() << "map:" << value.toObject().toVariantMap();
              }
              

              Output:

              bytes [{"name": "myexe", "installed": false, "id": "myexe@exe", "filename": "myexe", "type": "executable", "build_by_default": true}]
              doc: QJsonDocument([{"build_by_default":true,"filename":"myexe","id":"myexe@exe","installed":false,"name":"myexe","type":"executable"}])
              list: (QVariant(QVariantMap, QMap((build_by_default, QVariant(bool, true))(filename, QVariant(QString, myexe))(id, QVariant(QString, myexe@exe))(installed, QVariant(bool, false))(name, QVariant(QString, myexe))(type, QVariant(QString, executable)))))
              map: QMap(("build_by_default", QVariant(bool, true))("filename", QVariant(QString, "myexe"))("id", QVariant(QString, "myexe@exe"))("installed", QVariant(bool, false))("name", QVariant(QString, "myexe"))("type", QVariant(QString, "executable")))
              

              Cheers.

              Q Offline
              Q Offline
              Qjay
              wrote on 31 May 2018, 12:26 last edited by
              #6

              @Paul-Colby Thanks

              i myself did something like this :)

               QVariantMap json_map;
              
              
                  const QJsonDocument doc = QJsonDocument::fromJson(output);
                  qDebug().noquote() << doc;
                  qDebug().noquote() << doc.array();
                  foreach (auto value, doc.array()) {
                      qDebug().noquote() << value;
                      qDebug().noquote() << value.toObject();
                      qDebug().noquote() << value.toObject().value("name");
                      json_map["name"] = value.toObject().value("name").toString();
              
                      json_map["installed"] = value.toObject().value("installed").toBool();
              
                      json_map["id"] = value.toObject().value("id").toString();
              
                   
                      json_map["filename"] = value.toObject().value("filename").toString();
              
              
              
                  }
              
              1 Reply Last reply
              0

              1/6

              26 May 2018, 12:50

              • Login

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