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 to get the first and the last of QJsonObject ?

How to get the first and the last of QJsonObject ?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    I only need the first and the last QJsonObject, how to get in a easy way ?

    https://github.com/sonichy

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      is the QJsonObjects in a array ?
      if yes, you can just parse the file and get the elements from the QJsonArray.
      Unless they are in a array, there is really no concept of first and last.

      1 Reply Last reply
      2
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Use begin() for the first one and --end() for the last one and then the key()/value() methods of these iterators. Just make sure you don't go out of bounds (e.g. using count() or size()).

        mrjjM sonichyS 2 Replies Last reply
        2
        • Chris KawaC Chris Kawa

          Use begin() for the first one and --end() for the last one and then the key()/value() methods of these iterators. Just make sure you don't go out of bounds (e.g. using count() or size()).

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Chris-Kawa
          oh, you think he means first and last member of QJsonObject
          and not the first and last QJsonObject in the doc ?
          I read it as that :)

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            I don't know, could be both ;) Lets see what the OP says he wants.

            1 Reply Last reply
            0
            • Chris KawaC Chris Kawa

              Use begin() for the first one and --end() for the last one and then the key()/value() methods of these iterators. Just make sure you don't go out of bounds (e.g. using count() or size()).

              sonichyS Offline
              sonichyS Offline
              sonichy
              wrote on last edited by
              #6

              @Chris-Kawa said in How to get the first and the last of QJsonObject ?:

              iterators.

              I found iterator QJsonObject::begin() and iterator QJsonObject::end() can get them, but how to write the method completely

              https://github.com/sonichy

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by Chris Kawa
                #7

                Let's say you have JSON like this:

                {
                  "foo":  { "value" : 42 },
                  "bar":  { "value" : 53 },
                  "bazz": { "value" : 64 }
                }
                

                Using iterators you can do this:

                QJsonObject root = doc.object();
                
                QJsonObject first_object = root.begin().value().toObject();
                QJsonObject last_object = (--root.end()).value().toObject();
                

                Keep in mind that, as @mrjj pointed out, objects in JSON format are unordered, meaning that it's not specified which object is first or last (the order of text doesn't matter), so first_object is most likely not gonna be the "foo" object. In particular Qt's implementation keeps the objects in a map, so you will likely get alphabetical order of keys. Don't rely on this though.

                If you want an ordered structure the only option in JSON are arrays, so you might want something like this:

                [
                  { "foo":  42 },
                  { "bar":  53 },
                  { "bazz": 64 }
                ]
                

                And then access to first and last elements would look something like:

                QJsonArray root = doc.array();
                
                QJsonObject first_object = root.at(0).toObject();
                QJsonObject last_object  = root.at(root.count() - 1).toObject();
                

                You should of course add checks to verify that the root is an array, that it has any objects in it etc. I omitted those to make the point clear.

                1 Reply Last reply
                4

                • Login

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