在網絡請求的返回數據中,默認的NSLog打印的字典是這樣的
2016-12-22 10:59:40.100 cnblogs[4766:834950] {
message = "\U8be5\U7528\U6237\U4e0d\U5b58\U5728";
success = 0;
}
- 沒有雙引號
- 不能打印中文
- BOOL(true / false)值變成了0和1
- 如果有數組,數組是以
( )
小括號的方式打印,而不是[ ]
這樣的打印出的內容不能直接進行JSON格式化,解決方案是添加一個NSDictionary分類,將字典轉為JSON打印
@implementation NSDictionary (MTHJSONOutput)
- (NSString *)descriptionWithLocale:(id)locale {
NSString *output;
@try {
output = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
output = [output stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"]; // 處理\/轉義字符
} @catch (NSException *exception) {
output = self.description;
} @finally {
}
return output;
}
@end
添加分類前
添加分類前
添加分類后
添加分類前