iOS巅峰之NSString,NSData,NSArray 之间的转换


1.将数组转换成NSData

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dataArray options:NSJSONWritingPrettyPrinted error:nil];


2.将NSData转换成NSString

NSString *fileString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];


3.将字符串转换成NSData

NSData* aData= [str dataUsingEncoding: NSUTF8StringEncoding];


4.将NSData转换成(NSString, NSArray)

id jsonObject = [NSJSONSerialization JSONObjectWithData:aData options:NSJSONReadingAllowFragments error:nil];

NSArray *array = (NSArray *)jsonObject; // 或者 NSString *string = (NSString *)jsonObject;

你可能感兴趣的:(iOS,字符串,数组,字典,NSData,iOS,字符串,数组,字典,NSData)