I am using JSON Parsing in my application. My problem is when I get my response String from server , I parsed that string through JSON Parser and data in my dictionary. Parsing goes fine, but the ordering did not get maintained means when my string come it has "Learning Chinese" value first but when parsing that and getting data into dictionary then it did not come on first element.
I would like to explain this with my example code: String which I want to parse:
NSString *respStr = [req responseString];
NSLog(@"respStr=%@",respStr);
{
"Learning Chinese": [
{
"title": "Chinese nga28Traditionalnga27 Audio FlashCards for iPad",
"function_name": "Learning Chinese"
},
{
"title": "WCC Chinese Flashcards nga28Bigramnga27 with Audio",
"function_name": "Learning Chinese"
}
],
"Business & Office": [
{
"title": "Instant Customer",
"function_name": "Business & Office"
},
{
"title": "Phone Swipe nga7 Credit Card Terminal",
"function_name": "Business & Office"
}
],
"Kids": [
{
"title": "iWriteWords nga28Handwriting Gamenga27",
"function_name": "Kids"
},
{
"title": "SoundBook nga25 audio flashcards for toddlers",
"function_name": "Kids"
}
],
"Christmas": [
{
"title": "Santanga2s Fun",
"function_name": "Christmas"
},
{
"title": "Santerrific",
"function_name": "Christmas"
}
]
}
Now
NSMutableDictionary *dict=[respStr JSONValue];
NSLog(@"dict=%@",dict);
I got:
dict={
"Business & Office" = (
{
"function_name" = "Business & Office";
title = "Instant Customer";
},
{
"function_name" = "Business & Office";
title = "Phone Swipe nga7 Credit Card Terminal";
}
);
Christmas = (
{
"function_name" = Christmas;
title = "Santanga2s Fun";
},
{
"function_name" = Christmas;
title = Santerrific;
}
);
Kids = (
{
"function_name" = Kids;
title = "iWriteWords nga28Handwriting Gamenga27";
},
{
"function_name" = Kids;
title = "SoundBook nga25 audio flashcards for toddlers";
}
);
"Learning Chinese" = (
{
"function_name" = "Learning Chinese";
title = "Chinese nga28Traditionalnga27 Audio FlashCards for iPad";
},
{
"function_name" = "Learning Chinese";
title = "WCC Chinese Flashcards nga28Bigramnga27 with Audio";
}
);
}
After that:
NSLog(@"[dict allKeys]=%@",[dict allKeys]);
[dict allKeys]=(
Christmas,
Kids,
"Learning Chinese",
"Business & Office"
)
From the code it is clear that in string Learning Chinese is first but in dictionary it is on 4 number and when doing allKeys it comes on 3 number.
Please if anybody know why is this happening,tell me or any other way of solving it.Ordering is necessary for me.I have to show my data in the same order as in the string which is returned from server.
Any suggestions will be highly appreciated.
Thanks in advance.