在網(wǎng)絡(luò)請(qǐng)求的返回?cái)?shù)據(jù)中,默認(rèn)的NSLog打印的字典是這樣的
2016-12-22 10:59:40.100 cnblogs[4766:834950] {
message = "\U8be5\U7528\U6237\U4e0d\U5b58\U5728";
success = 0;
}
- 沒有雙引號(hào)
- 不能打印中文
- BOOL(true / false)值變成了0和1
- 如果有數(shù)組,數(shù)組是以
( )
小括號(hào)的方式打印,而不是[ ]
這樣的打印出的內(nèi)容不能直接進(jìn)行JSON格式化,解決方案是添加一個(gè)NSDictionary分類,將字典轉(zhuǎn)為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:@"/"]; // 處理\/轉(zhuǎn)義字符
} @catch (NSException *exception) {
output = self.description;
} @finally {
}
return output;
}
@end
添加分類前
添加分類前
添加分類后
添加分類前