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. Read from Json in an order
Forum Updated to NodeBB v4.3 + New Features

Read from Json in an order

Scheduled Pinned Locked Moved Solved General and Desktop
qt5jsonjsonparser
9 Posts 5 Posters 1.5k 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.
  • D Offline
    D Offline
    deleted286
    wrote on last edited by
    #1

    I have a QJsonArray. Here is my json file. How can i get my datas from array in an order?

    {
        "ucus plani": [
            {
                "bslngc": 0,
                "dgsm": 0,
                "rtno": 0,
                "tplm": 3
            },
            {
                "byl": 32.706642150878906,
                "enl": 40.0082893371582,
                "grv": 0,
                "irtf": 2500,
                "no": 0,
                "snrk": 1,
                "yrcp": 0
            },
            {
                "byl": 32.71247863769531,
                "enl": 39.98606491088867,
                "grv": 0,
                "irtf": 2500,
                "no": 1,
                "snrk": 2,
                "yrcp": 0
            },
            {
                "byl": 32.729129791259766,
                "enl": 40.00342559814453,
                "grv": 0,
                "irtf": 2500,
                "no": 2,
                "snrk": 0,
                "yrcp": 0
            }
        ]
    }
    
          int main(int argc, char *argv[])
      {
         QApplication a(argc, argv);
          QStringList fileNames;
           fileNames = QFileDialog::getOpenFileNames(nullptr,
                                              ("Uçuş Planı Seçiniz"), "/home/ilknur/yi/up.sakla", ("Uçuş Planı(*.upd)"));
    
             for (auto xfile : fileNames)
            {
    
        QFile file (xfile);
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        QByteArray data = file.readAll();
    
        QJsonDocument doc = QJsonDocument::fromJson(data);
    
        QJsonObject root = doc.object();
        QJsonArray planArray = root.value("ucus plani").toArray();
    
    
    for(const auto &value : planArray)
    {
      qDebug() <<  QJsonObject object = value.ToObject();
    
      }
        }
    
    KroMignonK 1 Reply Last reply
    0
    • D deleted286

      I have a QJsonArray. Here is my json file. How can i get my datas from array in an order?

      {
          "ucus plani": [
              {
                  "bslngc": 0,
                  "dgsm": 0,
                  "rtno": 0,
                  "tplm": 3
              },
              {
                  "byl": 32.706642150878906,
                  "enl": 40.0082893371582,
                  "grv": 0,
                  "irtf": 2500,
                  "no": 0,
                  "snrk": 1,
                  "yrcp": 0
              },
              {
                  "byl": 32.71247863769531,
                  "enl": 39.98606491088867,
                  "grv": 0,
                  "irtf": 2500,
                  "no": 1,
                  "snrk": 2,
                  "yrcp": 0
              },
              {
                  "byl": 32.729129791259766,
                  "enl": 40.00342559814453,
                  "grv": 0,
                  "irtf": 2500,
                  "no": 2,
                  "snrk": 0,
                  "yrcp": 0
              }
          ]
      }
      
            int main(int argc, char *argv[])
        {
           QApplication a(argc, argv);
            QStringList fileNames;
             fileNames = QFileDialog::getOpenFileNames(nullptr,
                                                ("Uçuş Planı Seçiniz"), "/home/ilknur/yi/up.sakla", ("Uçuş Planı(*.upd)"));
      
               for (auto xfile : fileNames)
              {
      
          QFile file (xfile);
          file.open(QIODevice::ReadOnly | QIODevice::Text);
          QByteArray data = file.readAll();
      
          QJsonDocument doc = QJsonDocument::fromJson(data);
      
          QJsonObject root = doc.object();
          QJsonArray planArray = root.value("ucus plani").toArray();
      
      
      for(const auto &value : planArray)
      {
        qDebug() <<  QJsonObject object = value.ToObject();
      
        }
          }
      
      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #2

      @suslucoder said in Read from Json in an order:

      How can i get my datas from array in an order?

      Which kind of order do mean?
      Values in JSON objects are unordered only the objects in an array are ordered (by index starting with 0), AFAIK.

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      D 1 Reply Last reply
      0
      • KroMignonK KroMignon

        @suslucoder said in Read from Json in an order:

        How can i get my datas from array in an order?

        Which kind of order do mean?
        Values in JSON objects are unordered only the objects in an array are ordered (by index starting with 0), AFAIK.

        D Offline
        D Offline
        deleted286
        wrote on last edited by
        #3

        @KroMignon for example, i want to see firsly

           {
                "byl": 32.706642150878906,
                "enl": 40.0082893371582,
                "grv": 0,
                "irtf": 2500,
                "no": 0,
                "snrk": 1,
                "yrcp": 0
            },
        

        which is the second part of my QJsonArray.

        Pablo J. RoginaP KroMignonK 2 Replies Last reply
        0
        • D deleted286

          @KroMignon for example, i want to see firsly

             {
                  "byl": 32.706642150878906,
                  "enl": 40.0082893371582,
                  "grv": 0,
                  "irtf": 2500,
                  "no": 0,
                  "snrk": 1,
                  "yrcp": 0
              },
          

          which is the second part of my QJsonArray.

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

          @suslucoder said in Read from Json in an order:

          which is the second part of my QJsonArray.

          As @KroMignon replied, the order is given but the way the JSON content is.
          So if you know you want to start with second element of the array (index 1) then go and start processing the array at such index...

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • D deleted286

            @KroMignon for example, i want to see firsly

               {
                    "byl": 32.706642150878906,
                    "enl": 40.0082893371582,
                    "grv": 0,
                    "irtf": 2500,
                    "no": 0,
                    "snrk": 1,
                    "yrcp": 0
                },
            

            which is the second part of my QJsonArray.

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #5

            @suslucoder said in Read from Json in an order:

            for example, i want to see firsly
            which is the second part of my QJsonArray.

            I can not understand what your problem is.
            when reading a JSON document with QJsonDocument::fromJson(), all items are parsed and stored.
            If you want to read the second item of an array, it is up to you:

            JsonDocument doc = QJsonDocument::fromJson(data);
            QJsonObject root = doc.object();
            QJsonArray planArray = root.value("ucus plani").toArray();
            
            QJsonObject secondItem = planArray[1].toObject();
            

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            D 1 Reply Last reply
            3
            • KroMignonK KroMignon

              @suslucoder said in Read from Json in an order:

              for example, i want to see firsly
              which is the second part of my QJsonArray.

              I can not understand what your problem is.
              when reading a JSON document with QJsonDocument::fromJson(), all items are parsed and stored.
              If you want to read the second item of an array, it is up to you:

              JsonDocument doc = QJsonDocument::fromJson(data);
              QJsonObject root = doc.object();
              QJsonArray planArray = root.value("ucus plani").toArray();
              
              QJsonObject secondItem = planArray[1].toObject();
              
              D Offline
              D Offline
              deleted286
              wrote on last edited by
              #6

              @KroMignon thank you for your answer. It works. After doing that,

              QJsonObject secondItem = planArray[1].toObject();
              

              How can i got the values in planArray[1] . It has some values like latitude longitude

              {
                   "byl": 32.706642150878906,
                   "enl": 40.0082893371582,
                   "grv": 0,
                   "irtf": 2500,
                   "no": 0,
                   "snrk": 1,
                   "yrcp": 0
               },
              

              can i call all of them separately

              jsulmJ 1 Reply Last reply
              -1
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by Christian Ehrlicher
                #7

                @suslucoder said in Read from Json in an order:

                How can i got the values in planArray[1] .

                You already answered this by your self e.g. here: https://forum.qt.io/topic/123496/how-to-parse-such-a-complex-json-file/5 and the other way round was explained here: https://forum.qt.io/topic/123484/creating-json-file-with-subroot/13

                Or simply read the documentation or the json savegame example which shows you exactly how to parse such simple stuff.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                4
                • D deleted286

                  @KroMignon thank you for your answer. It works. After doing that,

                  QJsonObject secondItem = planArray[1].toObject();
                  

                  How can i got the values in planArray[1] . It has some values like latitude longitude

                  {
                       "byl": 32.706642150878906,
                       "enl": 40.0082893371582,
                       "grv": 0,
                       "irtf": 2500,
                       "no": 0,
                       "snrk": 1,
                       "yrcp": 0
                   },
                  

                  can i call all of them separately

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @suslucoder said in Read from Json in an order:

                  can i call all of them separately

                  Of course you can! In the same way you do with any JSON object:

                  planArray[1].toObject()["enl"];
                  

                  Simply read https://doc.qt.io/qt-5/qjsonobject.html

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  D 1 Reply Last reply
                  2
                  • jsulmJ jsulm

                    @suslucoder said in Read from Json in an order:

                    can i call all of them separately

                    Of course you can! In the same way you do with any JSON object:

                    planArray[1].toObject()["enl"];
                    

                    Simply read https://doc.qt.io/qt-5/qjsonobject.html

                    D Offline
                    D Offline
                    deleted286
                    wrote on last edited by
                    #9

                    @jsulm thank you. I did it with your solution. It works fine.

                    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