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

Read from Json in an order

Scheduled Pinned Locked Moved Solved General and Desktop
qt5jsonjsonparser
9 Posts 5 Posters 1.4k 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.
  • D Offline
    D Offline
    deleted286
    wrote on 8 Feb 2021, 14:55 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();
    
      }
        }
    
    K 1 Reply Last reply 8 Feb 2021, 14:59
    0
    • D deleted286
      8 Feb 2021, 14:55

      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();
      
        }
          }
      
      K Offline
      K Offline
      KroMignon
      wrote on 8 Feb 2021, 14:59 last edited by KroMignon 2 Aug 2021, 15:00
      #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 8 Feb 2021, 15:02
      0
      • K KroMignon
        8 Feb 2021, 14:59

        @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 8 Feb 2021, 15:02 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.

        P K 2 Replies Last reply 8 Feb 2021, 15:06
        0
        • D deleted286
          8 Feb 2021, 15:02

          @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.

          P Offline
          P Offline
          Pablo J. Rogina
          wrote on 8 Feb 2021, 15:06 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
            8 Feb 2021, 15:02

            @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.

            K Offline
            K Offline
            KroMignon
            wrote on 8 Feb 2021, 15:08 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 9 Feb 2021, 05:57
            3
            • K KroMignon
              8 Feb 2021, 15:08

              @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 9 Feb 2021, 05:57 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

              J 1 Reply Last reply 9 Feb 2021, 06:03
              -1
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 9 Feb 2021, 06:01 last edited by Christian Ehrlicher 2 Sept 2021, 06:12
                #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
                  9 Feb 2021, 05:57

                  @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

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 9 Feb 2021, 06:03 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 9 Feb 2021, 06:35
                  2
                  • J jsulm
                    9 Feb 2021, 06:03

                    @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 9 Feb 2021, 06:35 last edited by
                    #9

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

                    1 Reply Last reply
                    0

                    9/9

                    9 Feb 2021, 06:35

                    • Login

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