开发者

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 anyon开发者_C百科e 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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜