JSON<=NSDictionary<=转换=>NSData类型

//NSDictionary  ==>NSData类型:
NSDictionary *targetDictionary;
NSMutableData *data = [[NSMutableData alloc] init]; 
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject : targetDictionary forKey : @"Custom Key”];
[archiver finishEncoding];
//NSData类型转换为NSDictionary类型:
NSData *data;
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSDictionary *myDictionary = [[unarchiver decodeObjectForKey:@"Custom Key"] retain];
[unarchiver finishDecoding];
//NSDictionary转换JSON:
NSString *jsonStr=[dict JSONRepresentation];


你可能感兴趣的:(json)