Write in JSON file - Data not inserted in the correct order
-
You should not rely on the order of items in a Json Object. According to the JSON standard: "An object is an unordered set of name/value pairs."
-
@HW-Developer
You ought not have such a requirement. JSON does not preserve the order of attributes of an object, and you should not need to.If you need to retain order put objects into a list/array.
-
This post is deleted!
-
@HW-Developer
To fully control the output order of the JSON you show you will have to stop saving as object/attribute and do whatever to produce something like the following JSON:[ // start list/array of objects { // start object "102": [ // start list of objects { // start object "NEUTRAL": { "blend": "100" } }, // end object { // start object "AE": { "blend": "100" } } // end object ] // end list of objects }, // end object { "105": [ { "NEUTRAL": { "blend": "100" } }, { "AE": { "blend": "100" } } ] } ] // end list of objects
See how all the objects have been individually put into lists where we need to control their order.
You will not be able to do these by directly serializing/deserializing your existing C++ objects. Either you must change their structure to match this or you must write code when serializing to produce this JSON format and similar on deserialization.
As you can see it's quite a bit of effort, and does not lend itself to readability. Which is why you should question why you ever need to control JSON output order like this, you are not intended to ever need to do so.
-
@HW-Developer
In the output I show youNEUTRAL
does come beforeAE
(because you put them into an array in that order) so you do get it back as you request. In your original for the second element you physically haveAE
beforeNEUTRAL
in the file, so you cannot help but read it back in that order. -
@JonB So there is no solution for my first post because I Want "Neutral" comes allways first even if I have other expression than "AE" like "SCh" for example :
{ "0": { "NEUTRAL": { "blend": "100" } }, "101": { "NEUTRAL": { "blend": "100" } }, "102": { "AE": { "blend": "100" }, "NEUTRAL": { "blend": "100" } }, "105": { "AE": { "blend": "100" }, "NEUTRAL": { "blend": "100" } }, "106": { "NEUTRAL": { "blend": "100" }, "SCh": { "blend": "100" } } }
-
@HW-Developer said in Write in JSON file - Data not inserted in the correct order:
So there is no solution for my first post
I, and @mchinand, have told you that you cannot control the order in which attributes/members of an object (e.g.
AE
&Neutral
here) are serialized in JSON, As you have already discovered for yourself. Only array/list items appear in a particular order, which is why you would need to change to using them if you must control the order.It does not matter how many times you ask the same question you will get the same answer. It's also why you simply should not, and should not have any need to, dictate the output order. If I were you I would get rid of why ever you have such a requirement, as we have already said.
-
@HW-Developer said in Write in JSON file - Data not inserted in the correct order:
So there is no solution for my first post because I Want "Neutral" comes allways first even if I have other expression than "AE" like "SCh"
no, because its not required by the standard,
You can of course write your very own json parser/writer and implement that behaviour yourself!
you could also use the source code of QJsonObject and change the key ordered QMap to an arbitrary ordered QHash without much hassle, (I think)
-
I did resolve it using rapidjson instead of QJsonObject
Thank you all