Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Present JSON string in sorted order from NSDictionary

I made one JSON string from the NSDictionary. The JSON string which I created doesn't come present the items in the order I entered keys and value pair in the NSDictionary .

Now I need all keys and value pairs returned as JSON string in alphabetic order. I tried lot of ways but not getting sorting of JSON string in alphabetic order.

Here's an example of the way the final string is being presented:

{
    "SubscriberID" : "603",
    "Amount" : "1",
    "MerchantID" : "100012",
    "Channel" : "Wallet",
    "RequestCode" : "0331",
    "PosID" : "0465F35F5577CUST",
    "TID" : "0000014",
    "Stan" : "NA"
}

How do I ensure the items are presented the way I entered them? Or how can I specify the items are alphabetically sorted?

like image 310
Prateek sharma Avatar asked Feb 19 '26 12:02

Prateek sharma


2 Answers

SBJson is able to do this, set sortKeys to YES on SBJsonWriter

http://superloopy.io/json-framework/

SBJsonWriter *writer = [[SBJsonWriter alloc] init];
writer.sortKeys = YES;
// Turn on humanReadable if you also need it pretty-printed
// writer.humanReadable = YES;
NSData *result = [writer dataWithObject:myDictionary];
like image 103
kevin Avatar answered Feb 21 '26 07:02

kevin


Like many key-value storage classes, NSDictionary does not guarantee order of the elements you've added. It is a mistake to assume that the key/value pairs, or keyset, will be returned with any particular order.

Further, JSON objects are unordered in the same fashion. You should not care about the order in which the objects are added. Nor should you (or your recipient) rely on being provided a JSON object with 'sorted' keys, as that's not really a valid concept with these particular structures. While ordering the keys might result in an iterator traversing the keys in the order you expect, it's not a guarantee, and should not be relied on.

I think you should revisit why you need the keys sorted in the first place, and see if you can find a way to avoid a dependency on their alphabetical ordering.

Edit: You mention the server requires an SHA hash of the JSON string. If you must, you can create a sorted JSON string by sorting the keys in an NSMutableArray, then creating the JSON string from those keys.

NSMutableArray *sortedKeys = [NSMutableArray arrayWithArray:[myDict allKeys]]; 
[sortedKeys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

NSMutableString *jsonString = [[NSMutableString alloc] init];
[jsonString appendString:@"{"];
for (NSString *key in sortedKeys) {
    [jsonString appendFormat:@"\"%@\"", key];
    [jsonString appendString:@":"];
    [jsonString appendFormat:@"\"%@\"", [myDict objectForKey:key]];
    [jsonString appendString:@","];
}
if ([jsonString length] > 2) {
    [jsonString deleteCharactersInRange:NSMakeRange([jsonString length] - 1, 1)];
}
[jsonString appendString:@"}"];

This code hasn't been tested, so you might need to play with it a bit. Thought it would be much better if you could find a JSON library to do this for you, although you might not have as much control over the key ordering.

like image 24
Craig Otis Avatar answered Feb 21 '26 06:02

Craig Otis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!