Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. India
  4. QT and JSON array
QtWS25 Last Chance

QT and JSON array

Scheduled Pinned Locked Moved Solved India
9 Posts 3 Posters 2.2k 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.
  • C Offline
    C Offline
    cnag
    wrote on last edited by
    #1

    Hi am new to the QT GUI development environment,
    Am facing an issue with the following snippet.
    All the necessary header files are included.

    QJsonDocument doc1;
    doc1 = QJsonDocument::fromJson("{ "
    " "Fruit_Parameters": "
    " { "
    " "Colour": Green, "
    " "Quantity": "350", "
    " "Origin": "India", "
    " "cat_ID": "00498D4AC", "
    " "Member_Id": "XYZ", "
    " "Business-cat": "Food", "
    " "Availablity": "yes" "
    " }, "
    " "
    " "Sender": "ABC", "
    " "
    ""Destination": "DEF" "
    " "
    " }");
    QByteArray data = doc1.toJson();
    QString jsonString = doc1.toJson(QJsonDocument::Compact);
    qDebug() << jsonString;

    This works perfect, however am getting the output in the following order.

    1. Sender : ABC
      2.Fruit parameters :{{ "
      " "Colour": Green, "
      " "Quantity": \350"", "
      " "Origin": "India", "
      " "cat_ID": "00498D4AC", "
      " "Member_Id": "XYZ", "
      " "Business-cat": "Food", "
      " "Availablity": "yes" "
      }
    2. "Destination":"DEF" "
      I have to send exact format of the json array in the above code snippet to server. Basically the order of the json array changes when i build the project.

    Thank you in advance ,
    cnag

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You do not have any array in your JSON code. An array is wrapped in square brackets: [ ]. What you pasted above are JSON objects, which - by definition - are in random order.

      Use arrays and the order will be preserved.

      (Z(:^

      C 1 Reply Last reply
      1
      • C Offline
        C Offline
        cnag
        wrote on last edited by
        #3

        Thank you @sierdzio . It worked for me .

        1 Reply Last reply
        0
        • sierdzioS sierdzio

          You do not have any array in your JSON code. An array is wrapped in square brackets: [ ]. What you pasted above are JSON objects, which - by definition - are in random order.

          Use arrays and the order will be preserved.

          C Offline
          C Offline
          cnag
          wrote on last edited by
          #4

          @sierdzio Hi again ,
          I used the Qjsonarray , however am getting additional [] brackets in the console output . How to remove the [] brackets before sending it to the server ?

          JonBJ 1 Reply Last reply
          0
          • C cnag

            @sierdzio Hi again ,
            I used the Qjsonarray , however am getting additional [] brackets in the console output . How to remove the [] brackets before sending it to the server ?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @cnag said in QT and JSON array:

            however am getting additional [] brackets in the console output

            What does this mean? qDebug() output? That may show [] around lists/arrays. Find out what the actual string is in the text generated/sent, not what might show in a console depending on what you are doing.

            1 Reply Last reply
            1
            • C Offline
              C Offline
              cnag
              wrote on last edited by
              #6

              Hello @JonB ,Yes am talking about qDebug() output.
              This is what i really meant.

              " [ \n {\n "Fruit-parameters": [ \n {\n "colour": Green\n },\n {\n "Quantity": "350"\n },\n {\n "Origin": "india"\n },\n {\n "cat-ID": "00498D4AC"\n },\n {\n "Member-Id": "XYZ"\n },\n {\n "Business-cat": "Food"\n }\n ] \n },\n {\n "Sender": "ABC"\n },\n {\n "Destination": "DEF"\n }\n ] \n"

              Due to these [] around the array , am getting a response as "Invalid Data format " from the server. Is it the behavior of Qjsonarray ???
              If i use the Qjsonobject am getting alphabetical order response in qDebug() output .(1. fruit_parameters 2. Destination 3.Sender).
              so i had gone with Qjsonarray .

              Thanks in advance

              JonBJ 1 Reply Last reply
              0
              • C cnag

                Hello @JonB ,Yes am talking about qDebug() output.
                This is what i really meant.

                " [ \n {\n "Fruit-parameters": [ \n {\n "colour": Green\n },\n {\n "Quantity": "350"\n },\n {\n "Origin": "india"\n },\n {\n "cat-ID": "00498D4AC"\n },\n {\n "Member-Id": "XYZ"\n },\n {\n "Business-cat": "Food"\n }\n ] \n },\n {\n "Sender": "ABC"\n },\n {\n "Destination": "DEF"\n }\n ] \n"

                Due to these [] around the array , am getting a response as "Invalid Data format " from the server. Is it the behavior of Qjsonarray ???
                If i use the Qjsonobject am getting alphabetical order response in qDebug() output .(1. fruit_parameters 2. Destination 3.Sender).
                so i had gone with Qjsonarray .

                Thanks in advance

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @cnag
                You asked for the object keys' order to be preserved. As a result @sierdzio told you to put them in an array so as to retain order. However, clearly your receiver is not going to accept an array when it expects a list of objects. So you should not serialize/send the whole array as an array, you should iterate through the elements and only send them. Or, if that is problematic, you can always strip the leading & trailing [ & ] off the generated JSON string before sending it. Then it won't arrive as an array.

                Having said the above, there should be no need to preserve the object order in the JSON in the first place. JSON objects are unordered, and your consumer should surely accept that, else it's not accepting proper JSON. So why you want to do this ordering is beyond me, but that's what you want to do if you want to preserve the order.

                1 Reply Last reply
                2
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  To add to what @JonB correctly pointed out: it seems that you have 2 arrays in your JSON now - one top-level and one for Fruit-parameters. You probably don't need the top-level one. Maybe then your consumer will accept it.

                  (Z(:^

                  1 Reply Last reply
                  1
                  • C Offline
                    C Offline
                    cnag
                    wrote on last edited by
                    #9

                    Thank you @sierdzio and @JonB , your suggestions has helped me out .I had to find what are the string values to be sent through Qjsonarray and then iterate through them , followed by some string techniques to retain the order .Finally i was able to get the response's from the consumer !!!

                    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