IOS开发解决控制台打印JSON数据中文乱码

将这个文件直接拖动到项目即可生效:
文件名:Foundation+JSONLog.m

#ifdef DEBUG

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

#pragma mark - 方法交换

static inline void wy_swizzleSelector(Class class, SEL originalSelector, SEL swizzledSelector) {
    
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    
    BOOL didAddMethod = class_addMethod(class,
                                        originalSelector,
                                        method_getImplementation(swizzledMethod),
                                        method_getTypeEncoding(swizzledMethod));
    if (didAddMethod) {
        class_replaceMethod(class,
                            swizzledSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}


#pragma mark - NSObject分类

@implementation NSObject (JSONLog)
//将obj转换成json字符串。如果失败则返回nil.
- (NSString *)wy_convertToJsonString {
    
    //先判断是否能转化为JSON格式
    if (![NSJSONSerialization isValidJSONObject:self])  return nil;
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted  error:&error];
    if (error || !jsonData) return nil;
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}
@end


#pragma mark - NSDictionary分类

@implementation NSDictionary (JSONLog)

//用此方法交换系统的 descriptionWithLocale: 方法。该方法在代码打印的时候调用。
- (NSString *)wy_jsonlog_descriptionWithLocale:(id)locale{
    
    NSString *result = [self wy_convertToJsonString];//转换成JSON格式字符串
    if (!result) return [self wy_jsonlog_descriptionWithLocale:locale];//如果无法转换,就使用原先的格式
    return result;
}
//用此方法交换系统的 descriptionWithLocale:indent:方法。功能同上。
- (NSString *)wy_jsonlog_descriptionWithLocale:(id)locale indent:(NSUInteger)level {
    
    NSString *result = [self wy_convertToJsonString];
    if (!result) return [self wy_jsonlog_descriptionWithLocale:locale indent:level];
    return result;
}

//用此方法交换系统的 debugDescription 方法。该方法在控制台使用po打印的时候调用。
- (NSString *)wy_jsonlog_debugDescription{
    
    NSString *result = [self   wy_convertToJsonString];
    if (!result) return [self  wy_jsonlog_debugDescription];
    
    return result;
}
//在load方法中完成方法交换
+ (void)load {
    
    //方法交换
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        Class class = [self class];
        wy_swizzleSelector(class, @selector(descriptionWithLocale:), @selector(wy_jsonlog_descriptionWithLocale:));
        wy_swizzleSelector(class, @selector(descriptionWithLocale:indent:), @selector(wy_jsonlog_descriptionWithLocale:indent:));
        wy_swizzleSelector(class, @selector(debugDescription), @selector(wy_jsonlog_debugDescription));
        
    });
}

@end


#pragma mark - NSArray分类

@implementation NSArray (JSONLog)

//用此方法交换系统的 descriptionWithLocale: 方法。该方法在代码打印的时候调用。
- (NSString *)wy_jsonlog_descriptionWithLocale:(id)locale{
    
    NSString *result = [self wy_convertToJsonString];
    if (!result) return [self wy_jsonlog_descriptionWithLocale:locale];
    return result;
}
//用此方法交换系统的 descriptionWithLocale:indent:方法。功能同上。
- (NSString *)wy_jsonlog_descriptionWithLocale:(id)locale indent:(NSUInteger)level {
    
    NSString *result = [self wy_convertToJsonString];
    if (!result) return [self wy_jsonlog_descriptionWithLocale:locale indent:level];
    return result;
}
//用此方法交换系统的 debugDescription 方法。该方法在控制台使用po打印的时候调用。
- (NSString *)wy_jsonlog_debugDescription{
    
    NSString *result = [self wy_convertToJsonString];
    if (!result) return [self wy_jsonlog_debugDescription];
    return result;
}

+ (void)load {
    
    //方法交换
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        Class class = [self class];
        wy_swizzleSelector(class, @selector(descriptionWithLocale:), @selector(wy_jsonlog_descriptionWithLocale:));
        wy_swizzleSelector(class, @selector(descriptionWithLocale:indent:), @selector(wy_jsonlog_descriptionWithLocale:indent:));
        wy_swizzleSelector(class, @selector(debugDescription), @selector(wy_jsonlog_debugDescription));
    });
}

@end


#endif

之后控制台就会打印正常的中文了:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄油奥特曼

如果可以,请我喝一杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值