Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QT and JSON array



  • 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


  • Moderators

    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.



  • Thank you @sierdzio . It worked for me .



  • @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 ?



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



  • 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



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


  • Moderators

    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.



  • 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 !!!


Log in to reply