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. QJsonObject: Access Element tree
Forum Updated to NodeBB v4.3 + New Features

QJsonObject: Access Element tree

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.1k 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.
  • F Offline
    F Offline
    fem_dev
    wrote on 19 Aug 2019, 17:40 last edited by
    #1

    Looking this JSON example below, how can I access the value of this JSON property called c using Qt 5 JSON API?

    {
      "my_array": [
        {
          "a": {
            "b": {
              "c": 10
            }
          }
        },
        {
          "a": {
            "b": {
              "c": 20
            }
          }
        }
      ]
    }
    

    I was thinking that could be something like this:

    // Get the first object:
    int c0 = my_json["my_array"][0]["a"]["b"]["c"].toInt();
    // c0 = 10;
    
    // Get the second object:
    int c1 = my_json["my_array"][1]["a"]["b"]["c"].toInt();
    // c1 = 20;
    

    How can I get this c0 ans c1 values?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MrShawn
      wrote on 19 Aug 2019, 18:05 last edited by
      #2

      First you need to use QJsonDocument to get the json from text.

      Once you get it into a document, start breaking it apart. From your structure you want an object first. You get the value from your object using your key.

      Since it is a little hard to explain here is a small example which could help get you started. I read your JSON from a file test.json and prints out "C0" and "C1" like you are asking.

      #include <QApplication>
      #include <QFile>
      #include <QJsonDocument>
      #include <QJsonArray>
      #include <QJsonObject>
      
      #include <QDebug>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      //    MainWindow w;
      //    w.show();
      
          QFile mFile("test.json");
          if (!mFile.open(QIODevice::ReadOnly))
          {
              qDebug() << "Unable to open file";
              return 0;
          }
      
          QJsonDocument mDoc;
          mDoc = QJsonDocument::fromJson(mFile.readAll());
          if (!mDoc.isObject())
          {
              qDebug() << "JSON not formatted properly or is not an object.";
              return 0;
          }
      
          qDebug() << mDoc.object().value("my_array").toArray().size();
          
          int c0 = mDoc.object().value("my_array").toArray().at(0).toObject().value("a").toObject().value("b").toObject().value("c").toInt();
          int c1 = mDoc.object().value("my_array").toArray().at(1).toObject().value("a").toObject().value("b").toObject().value("c").toInt();
          qDebug() << c0 << c1;
      
          return a.exec();
      }
      
      1 Reply Last reply
      4
      • F Offline
        F Offline
        fem_dev
        wrote on 19 Aug 2019, 18:09 last edited by fem_dev
        #3

        @mrshawn thank you so much! That's perfect!

        1 Reply Last reply
        0

        1/3

        19 Aug 2019, 17:40

        • Login

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