0

I currently have an ordered JSON string being passed into my iPhone app, which is then being parsed using the JSON Framework.

The data is as follows:

"league_table": object{
"Premiership": array[6],
"Championship": array[6],
"Division 1": array[6],
"Division 2": array[6],
"Division 3": array[6]
}

However when it parses that, it throws out a weird order.

Division 2
Division 1
Championship
"Division 3"
Premiership

Which I got by calling : NSLog(@"%@",[dictionaryValue allKeys]);.

Has anyone experienced this before? Any idea what to do to sort it again?

UPDATE ::

The shortened UN-Parsed JSON is here :

{"league_table":
{
"Premiership":[],
"Championship":[],
"Division 1":[],
"Division 2":[],
"Division 3":[]}
}

As far as I can tell, this is a Key/Value Pair, so it should be parsed in the same order. For instance going to http://json.parser.online.fr/ and pasting that in will parse it in the correct order. However the JSON-Framework doesn't parse it the same, it parses it in a strange order with no real sorting going on

2 Answers 2

4

JSON object fields don't have a defined order. If you want key/value pairs in a defined order, there are basically two options:

  1. An array of single-field objects:

    [{"Premiership": array[6]},
     {"Championship": array[6]},
     {"Division 1": array[6]},
     {"Division 2": array[6]},
     {"Division 3": array[6]}]
    
  2. An array of key/value pairs:

    [["Premiership", array[6]],
     ["Championship", array[6]],
     ["Division 1", array[6]],
     ["Division 2", array[6]],
     ["Division 3", array[6]]]
    

Sidenote: I'm half-guessing that the relationship between the sample data and JSON. I don't know what object and array[6] are doing there.

Sign up to request clarification or add additional context in comments.

4 Comments

Im not quite sure what you mean by this, the setup looks fine its just the way the JSON-Framework parses it ruins the order its sent in.
The whole point of key/value is not having to know the index to find elements in an array. You can get at the data out-of-order by using key names. There is never a guarantee that the data will remain in a specific order, unless its in an indexed array.
You can always order it when the order becomes important, like sending it off somewhere, by referring to the keys in order. While in the dictionary, though, there is no order, the effective equivalent of being in random order.
@Danny, @Matthew: JSON specifically defines objects as unordered sets of name/value pairs (which is strictly incorrect, since {"a":1, "a":2} is an unordered set of name/value pairs, but is clearly not an object; nonetheless, the key point here is that it is unordered). The fact that it necessarily appears in some order when serialised is neither here nor there. You can't rely on the apparent order. Some libraries extend JSON by allowing an order-preserving serialisation from some kind of ordered-dictionary type, but I think it's dangerous to depend on this.
0

Yep, I've seen that before.

Does this cause you a problem? it looks like a Dictionary (keyed) .. so I guess your application just accesses the required elements by it's key.

I believe a JSON array format would maintain the order.

EDIT

Ok, so you need to maintain the order ... but from what you say, it looks like the key isn't important.

Are you in control of the creation of the JSON string ? If so, could you present the data like this :

[
{
"leagueName":"Premiership",
"leagueLines":[...]
},
{
"leagueName":"Championship",
"leagueLines":[...]
},
{
"leagueName":"League One",
"leagueLines":[]
},
 ... etc ....
]

I've put in the leagueName in just for information ... who know's you might need it for something :)

Good Luck

4 Comments

Hey, The problem is i'm then Using a ForEach loop to get the keys out then iterating over the children below it. It sort of needs that order to be the same otherwise it outputs them in the wrong order
Thanks! This looks like it could work, i'll get the guys to try this one out tomorrow.
Hey Danny, How did you get on ? Is using an array instead of a dictionary working out for you ?
Works brilliantly mate, just got it all working. Thanks for all of your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.