Unsolved How json sort values
-
@firsnur96
Show your code for exactly what you mean by "array". As @jsulm wrote JSON should output true arrays in array order. -
Json is by definition unordered
However, the QT implementation uses a QMap (IIRC). QMap alphabetically orders its keys, nothing you can do about it, but writing your own json writer
-
@J-Hilk
I'm lost. If the OP is genuinely JSON-serializing an array, in C++ terms (or list in Python), there are no "keys", alphabetical or not. Only the integer indexes into the array. Which it will output to the JSON in array element order. So what are you talking about here for "array", and as I asked the OP to show code, there should be noQMap
nor "cherry", "apple", "banana"?Or it's not an "array"....
-
@JonB said in How json sort values:
I'm lost. If the OP is genuinely JSON-serializing an array, in C++ terms (or list in Python), there are no "keys", alphabetical or not.
Of course there are keys in JSON, for example:
{ "index": 1, "value": "test", "parameters": ["x", "b", "c"'] }
There are 3 keys:
index
,value
andparameters
.
Those will in a QMap, which means they are sorted.
But the value hold by keyparameters
will not be sorted, this is a list. -
@JonB I was refering to the opening post:
I have a json file and i read some values on there.
My question is, how qt sort the json elements?this is ambiguous enough to mean anything I may guess it to the qt way of alphabetically sorting keys.
the 2nd post
Cherry
Apple
Bananaıt sorts them
apple
banana
cherrydoesn't add anything of value, those are neither lists nor arrays in any language/storage format that I know of
-
@KroMignon said in How json sort values:
@JonB said in How json sort values:
I'm lost. If the OP is genuinely JSON-serializing an array, in C++ terms (or list in Python), there are no "keys", alphabetical or not.
Of course there are keys in JSON, for example:
Did you read my answer? I italicised array. What you show with
{
...}
is not an array, it's an object. You do have one one array value there,["x", "b", "c"']
. And that should be serialized just as shown, there are no keys here.There are 3 keys:
index
,value
andparameters
.Those will in a QMap, which means they are sorted.
Indeed. And they do not form an array or list.
But the value hold by key
parameters
will not be sorted, this is a list.Indeed. Which is what I & @jsulm said:
There is no sort order in JSON. Array elements come in the order they were added to the array.
-
@JonB said in How json sort values:
Did you read my answer? I italicised array. What you show with { ... } is not an array, it's an object. You do have one one array value there, ["x", "b", "c"']. And that should be serialized just as shown, there are no keys here.
I apologies, I am not a native speaker.
I did read your answer, but it seems I did not right understand it.
I know that{}
is for objects and[]
for arrays.
I also know that many people mixup keys and values when speaking about JSON, and I am pretty sure this is the root failure here for this thread. -
@KroMignon
I think you are right. I asked the OP for clarification of the code, because "sorting" of "keys" with values "apple", "banana" & "cherry" refers to a "map" or "object" but not to an "array". -
"Fruits":[ { "0":[ "0", "Cherry", "---" ], "1":[ "1", "Apple", "---" ], "2":[ "2", "Banana", "---" ]
Here is a part of my json file, there should me missing paranthesis etc. sorry for it. Im trying to understand all answers
-
@firsnur96 See the answer from @J-Hilk
-
@firsnur96
I don't know whether what you have shown is what you have, what you want, what you don't get, or whatever.Each item like
[ "0", "Cherry", "---" ],
is itself an array/list. But the outer element like
{ "0": ... "1": ...
is an object with keys like
"0"
,"1"
.I don't know where your
ıt sorts them
apple
banana
cherry
comes from.
Maybe some structure in C++ is involved? A
QMap
?? I don't understand what exactly is "missorting" where.