ios 把字符串转换成json字符串
有时服务器要json的字符串 这是就需要把字符串转换成json字符串的方法
操作方法
- 01
把nsstring转化成jsonStr -(NSString *)JSONString:(NSString *)aString { NSMutableString *s = [NSMutableString stringWithString:aString]; [s replaceOccurrencesOfString:@"\"" withString:@"\\\"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])]; [s replaceOccurrencesOfString:@"/" withString:@"\\/" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])]; [s replaceOccurrencesOfString:@"\n" withString:@"\\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])]; [s replaceOccurrencesOfString:@"\b" withString:@"\\b" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])]; [s replaceOccurrencesOfString:@"\f" withString:@"\\f" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])]; [s replaceOccurrencesOfString:@"\r" withString:@"\\r" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])]; [s replaceOccurrencesOfString:@"\t" withString:@"\\t" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])]; return [NSString stringWithString:s]; }
- 02
创建一个工程
- 03
创建一个字符串
- 04
吧方法创建好
- 05
黏贴到方法里,请忽视警告
- 06
打印结果
- 07
结果出来了