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 set Json object to Json document without sorting the keys alphabetically
Forum Update on Monday, May 27th 2025

How to set Json object to Json document without sorting the keys alphabetically

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 5.8k 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
    flopoe
    wrote on 29 Jan 2015, 07:57 last edited by
    #1

    I insert some keys and vals to my Json object like this: @jObj.insert(keyName, val);@

    After inserting some key and val pairs to my object I am trying to set the object to Json document with: "void QJsonDocument::setObject(const QJsonObject & object)":http://doc-snapshot.qt-project.org/qt5-5.4/qjsondocument.html#setObject

    However this method sorts the keys alphabetically and than puts them into Json document. But I need an unsorted Json document .

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 29 Jan 2015, 08:00 last edited by
      #2

      a simple question: why? :)

      I am asking because it's not specified by the JSON standard that the input order should be the same like the output order. And for applications using the JSON data it also doesn't make a difference how the order is.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • F Offline
        F Offline
        flopoe
        wrote on 29 Jan 2015, 08:06 last edited by
        #3

        Because I have a database with lots of columns on server side and I want to bind the values in a for loop easily that I got from the client and insert them into server database. Also I want to show the Json document exactly like my database. So I need the same order in database.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 29 Jan 2015, 08:21 last edited by
          #4

          [quote author="flopoe" date="1422518779"]Because I have a database with lots of columns on server side and I want to bind the values in a for loop easily that I got from the client and insert them into server database.
          [/quote]

          But this is a matter how you design your for loop. For example you can do this with no problem:

          @
          foreach( QString key, myJsonObject->keys() ) {
          // pseudo code
          dbQuery.updateColumn( key, myJsonOject.value(key) );
          }
          @

          So the database query also isn't dependent of the order actually.

          [quote author="flopoe" date="1422518779"]
          Also I want to show the Json document exactly like my database. So I need the same order in database.[/quote]

          Again, for what purpose? It is simply not possible to guarantee this because of the internal data structures used in the Qt implementation. Most probably because of performance reason and because there is just no use case where this is mandatory.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • F Offline
            F Offline
            flopoe
            wrote on 29 Jan 2015, 08:35 last edited by
            #5

            Yes I know there are many ways to do it using different methods but I've just wondered this. Thank you!

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on 29 Jan 2015, 10:47 last edited by
              #6

              It is quite simple. A JSON object is like a map: a set of keys referencing values, with no inherent order. If you need a specific order, you should not use an object, but a list (of objects).

              1 Reply Last reply
              0

              4/6

              29 Jan 2015, 08:21

              • Login

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