iOS中使用NSJSONSerialization把對象轉(zhuǎn)為JSON字符串后渐苏,多出來反斜杠的問題
代碼
NSDictionary *dic = @{@"url": @"http://..."};
NSLog(@"%@", dic);
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
NSLog(@"%@, json object = %@", jsonString, jsonDict);
執(zhí)行結(jié)果:
2016-12-15 16:49:45.009 test[14989:546596] {
url = "http://...";
}
2016-12-15 16:49:45.010 test[14989:546596] {"url":"http://..."}, json object = {
url = "http://...";
}
轉(zhuǎn)換后的json字符串中url地址被轉(zhuǎn)義了 :(
使用字符串替換可以事后彌補:
[jsonString stringByReplacingOccurrencesOfString:@"\" withString:@""];