美文网首页
服务器返回的Json数据,中文出现乱码问题

服务器返回的Json数据,中文出现乱码问题

作者: 策马鞭程 | 来源:发表于2016-04-29 10:53 被阅读866次
    乱码贴图

    解决方法:
    添加一个分类文件即可
    新建一个分类命名Foundation+Log.m

    #import <Foundation/Foundation.h>
    
    @implementation NSDictionary (Log)
    - (NSString *)descriptionWithLocale:(id)locale
    {
        NSMutableString *str = [NSMutableString string];
        
        [str appendString:@"{\n"];
        
        // 遍历字典的所有键值对
        [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            [str appendFormat:@"\t%@ = %@,\n", key, obj];
        }];
        
        [str appendString:@"}"];
        
        // 查出最后一个,的范围
        NSRange range = [str rangeOfString:@"," options:NSBackwardsSearch];
        if (range.length != 0) {
            // 删掉最后一个,
            [str deleteCharactersInRange:range];
        }
        
        return str;
    }
    @end
    
    @implementation NSArray (Log)
    - (NSString *)descriptionWithLocale:(id)locale
    {
        NSMutableString *str = [NSMutableString string];
        
        [str appendString:@"[\n"];
        
        // 遍历数组的所有元素
        [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [str appendFormat:@"%@,\n", obj];
        }];
        
        [str appendString:@"]"];
        
        // 查出最后一个,的范围
        NSRange range = [str rangeOfString:@"," options:NSBackwardsSearch];
        if (range.length != 0) {
            // 删掉最后一个,
            [str deleteCharactersInRange:range];
        }
        
        return str;
    }
    @end```
    
    ##完成,打印试试
    
    
    ![恢复正常](https://img.haomeiwen.com/i680796/bff0e4f4226e5516.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    

    相关文章

      网友评论

          本文标题:服务器返回的Json数据,中文出现乱码问题

          本文链接:https://www.haomeiwen.com/subject/mmghrttx.html