iOS Json文件转Object(自动检测文本字符编码)

-(nullable id)jsonObjectFromJsonFile:(NSString *)filePath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:filePath]) {
        return nil;
    }
    
    NSArray *arrEncoding = @[@(NSASCIIStringEncoding),
                             @(NSNEXTSTEPStringEncoding),
                             @(NSJapaneseEUCStringEncoding),
                             @(NSUTF8StringEncoding),
                             @(NSISOLatin1StringEncoding),
                             @(NSSymbolStringEncoding),
                             @(NSNonLossyASCIIStringEncoding),
                             @(NSShiftJISStringEncoding),
                             @(NSISOLatin2StringEncoding),
                             @(NSUnicodeStringEncoding),
                             @(NSWindowsCP1251StringEncoding),
                             @(NSWindowsCP1252StringEncoding),
                             @(NSWindowsCP1253StringEncoding),
                             @(NSWindowsCP1254StringEncoding),
                             @(NSWindowsCP1250StringEncoding),
                             @(NSISO2022JPStringEncoding),
                             @(NSMacOSRomanStringEncoding),
                             @(NSUTF16StringEncoding),
                             @(NSUTF16BigEndianStringEncoding),
                             @(NSUTF16LittleEndianStringEncoding),
                             @(NSUTF32StringEncoding),
                             @(NSUTF32BigEndianStringEncoding),
                             @(NSUTF32LittleEndianStringEncoding)
    ];
            
       /*
    NSArray *arrEncodingName = @[@"NSASCIIStringEncoding",
                                 @"NSNEXTSTEPStringEncoding",
                                 @"NSJapaneseEUCStringEncoding",
                                 @"NSUTF8StringEncoding",
                                 @"NSISOLatin1StringEncoding",
                                 @"NSSymbolStringEncoding",
                                 @"NSNonLossyASCIIStringEncoding",
                                 @"NSShiftJISStringEncoding",
                                 @"NSISOLatin2StringEncoding",
                                 @"NSUnicodeStringEncoding",
                                 @"NSWindowsCP1251StringEncoding",
                                 @"NSWindowsCP1252StringEncoding",
                                 @"NSWindowsCP1253StringEncoding",
                                 @"NSWindowsCP1254StringEncoding",
                                 @"NSWindowsCP1250StringEncoding",
                                 @"NSISO2022JPStringEncoding",
                                 @"NSMacOSRomanStringEncoding",
                                 @"NSUTF16StringEncoding",
                                 @"NSUTF16BigEndianStringEncoding",
                                 @"NSUTF16LittleEndianStringEncoding",
                                 @"NSUTF32StringEncoding",
                                 @"NSUTF32BigEndianStringEncoding",
                                 @"NSUTF32LittleEndianStringEncoding"
    ];
         */
    for (int i = 0 ; i < [arrEncoding count]; i++) {
        
        unsigned long encodingCode = [arrEncoding[i] unsignedLongValue];
        NSError *error = nil;
        NSString *aString = [NSString stringWithContentsOfFile:filePath encoding:encodingCode error:&error];
        if (!error) {
             
            NSData *data = [aString dataUsingEncoding:encodingCode];
            NSString *string = [[NSString alloc] initWithData:data encoding:encodingCode];
            if (string && ![string isEqualToString:@""]) {
                
//                NSLog(@"文本字符编码格式:%@",arrEncodingName[i]);
                NSData *jsonData = [string dataUsingEncoding:NSUTF8StringEncoding];
                NSError *error;
                id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
                return jsonObject;
            }
        }
        

    }
    
    return nil;
}

你可能感兴趣的:(iOS Json文件转Object(自动检测文本字符编码))