Keeping the Order of Nsdictionary Keys When Converted to Nsdata with [Nsjsonserialization Datawithjsonobject:]

Keeping the order of NSDictionary keys when converted to NSData with [NSJSONSerialization dataWithJSONObject:]

I use OrderedDictionary from CocoaWithLove whenever I need to keep the order of my dictionary keys.

Basically, OrderedDictionary contains an NSMutableDictionary and an NSMutableArray for the keys inside it, to keep track of the order of the keys. It implements the required methods for subclassing NSDictionary/NSMutableDictionary and just "passes" the method call to the internal NSMutableDictionary.

Keeping the order of NSDictionary keys when converted to NSData with [NSJSONSerialization dataWithJSONObject:]

I use OrderedDictionary from CocoaWithLove whenever I need to keep the order of my dictionary keys.

Basically, OrderedDictionary contains an NSMutableDictionary and an NSMutableArray for the keys inside it, to keep track of the order of the keys. It implements the required methods for subclassing NSDictionary/NSMutableDictionary and just "passes" the method call to the internal NSMutableDictionary.

JSON NSDictionary disordered after AFJSONResponseSerializer

NSDictionary in Cocoa does not keep order of elements. So using AFJSONResponseSerializer it is impossible to keep the keys in the same order they are in JSON file. You have to parse JSON yourself or change JSON structure to use NSArray.

For example:

{
[
{"name" : "first key", "value" : [val1, val2, val3]},
{"name" : "second key", "value" : [val1, val2, val3]},
{"name" : "third key", "value" : [val1, val2, val3]},
{"name" : "fourth key", "value" : [val1, val2, val3]}
]
}

UPDATE:

This thread could be useful for you Keeping the order of NSDictionary keys when converted to NSData with [NSJSONSerialization dataWithJSONObject:]

JSON data not creating from NSDictionary

It looks like you're adding an NSDate or an NSURL object into your dictionary to be converted to JSON. The problem is that JSON doesn't have a "Date" or a "URL" data type. So you need to convert the date / URL into a string (NSString) and add that instead.

How can 2 NSData objects output the same HEX but be different when converted to char array?

You're not printing the data in the buffers. You're printing the pointers which the addresses of the two local arrays decay into. If you actually print the data, they will be the same:

NSMutableString *hex = [NSMutableString string], *secondHex = [NSMutableString string];
for (int i = 0; i < sizeof(cData); i++) {
[hex appendFormat:@"%02x", cData[i]];
[secondHex appendFormat:@"%02x", cSecondData[i]];
}

NSLog(@"%@ %@", hex, secondHex);

Convert nsdictionary to nsdata

You can use an NSKeyedArchiver to serialize your NSDictionary to an NSData object. Note that all the objects in the dictionary will have to be serializable (implement NSCoding at some point in their inheritance tree) in order for this to work.

Too lazy to go through my projects to lift code, so here is some from the Internet:

Encode

NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:yourDictionary forKey:@"Some Key Value"];
[archiver finishEncoding];
[archiver release];
/** data is ready now, and you can use it **/
[data release];

Decode:

NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSDictionary *myDictionary = [[unarchiver decodeObjectForKey:@"Some Key Value"] retain];
[unarchiver finishDecoding];
[unarchiver release];
[data release];

Pass NSDate in NSDictionary and convert into NSData for jsondata fix

NSDate cannot be represented in JSON natively. As NSJSONSerialization documentation says:

An object that may be converted to JSON must have the following properties:

  • The top level object is an NSArray or NSDictionary.

  • All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.

  • All dictionary keys are instances of NSString.

  • Numbers are not NaN or infinity.

It's unclear from your question in what format the date should be represented in your JSON. Often, you format dates in some standard date string format (e.g. ISO 8601/RFC 3339 format like 2016-01-25T06:54:00Z).

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZZ";

Or, for macOS 10.12 and iOS 10, you can do:

NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];

Then:

NSString *birthDateString = [formatter stringFromDate:birthDate];

See Apple Technical Q&A 1480 for more information.

Or perhaps, because it's a birthday, you only need yyyy-MM-dd.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
formatter.dateFormat = @"yyyy-MM-dd";
NSString *birthDateString = [formatter stringFromDate:birthDate];

Bottom line, you have to tell us what format your web service is expecting the dates to be formatted, and then we can help you format it accordingly.



Related Topics



Leave a reply



Submit