Search code examples
iphoneobjective-cjsonjson-framework

JSON-Framework Parsing data in a strange order


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


Solution

  • 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