NSDictionary、NSString相互转化

NSDictionary 怎么转成NSString

NSDictionary *infoDict = @{@"name":@"taoyali",@"password":@"taoyali"};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:infoDict options:0 error:0];
NSString *dataStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];


JSON字符串转化为字典

+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败:%@",err);
        return nil;
    }
    return dic;
}  

你可能感兴趣的:(NSDictionary、NSString相互转化)