首页 > 代码库 > NSData 转换 Dictionary
NSData 转换 Dictionary
#import "NSDataToDictionaryTools.h"@implementation NSDataToDictionaryTools+(NSData *)NSDictionaryToNSData:(NSDictionary *)dictionary{ NSMutableData *data =http://www.mamicode.com/ [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:dictionary forKey:@"KeyValue"]; [archiver finishEncoding]; return data;}+(NSString *)NSDictionaryToJSON:(NSDictionary *)dictionary{ NSData *prettyJson = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil]; NSString *jsonstr = [self NSDataToNSString:prettyJson]; return jsonstr;}+(NSDictionary *)NSDataToNSDictionary:(NSData *)data{ //NSData-->NSDictionary NSDictionary *myDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; //NSData-->NSDictionary// NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];// NSDictionary *myDictionary = [unarchiver decodeObjectForKey:@"KeyValue"];// [unarchiver finishDecoding]; return myDictionary;}//NSString和NSData转换(即可作json串互相转换)+(NSString *)NSDataToNSString:(NSData *)data{ //NSData-> NSString NSString *myString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; return myString; //NSString->NSData //NSString *aString = @"我是NSString"; //NSData *testData = http://www.mamicode.com/[testString dataUsingEncoding: NSUTF8StringEncoding];}@end
NSData 转换 Dictionary
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。