NSJSONSerialization iOS自带解析json

前端之家收集整理的这篇文章主要介绍了NSJSONSerialization iOS自带解析json前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

特殊字符转义

//特殊字符转化

+(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];

}

//我这边的后台有点奇葩 所以要先转字符串在转特殊字符然后在转nsdara 弄了一大圈回到了原点 ,我也知道有点奇葩,但我这里就只能这么转,木有办法

NSData *response = [[HSAssist JSONString:[[NSString alloc] initWithData:[request responseData] encoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:nil]; NSLog(@"%@",json);

猜你在找的Json相关文章