iOS-JSON空值处理

服务端返回的数据经常会有空值,如果没有处理好的话,会出现闪退的情况,可以写一个NSDictionary的扩展对空值进行处理,防止闪退

//
//  NSDictionary+MyDictionary.m
//  yunbaolive
//
//  Created by apple on 2021/9/24.
//  Copyright © 2021 cat. All rights reserved.
//

#import "NSDictionary+MyDictionary.h"

@implementation NSDictionary (MyDictionary)


- (NSDictionary *)deleteAllNullValue{
    NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] init];
    for (id keyStr in self.allKeys) {
        if ([[self objectForKey:keyStr] isKindOfClass:[NSDictionary class]]) {
            NSMutableDictionary *dicSub = [[NSMutableDictionary alloc] initWithDictionary:[self objectForKey:keyStr]];
            for (NSString *sub in dicSub.allKeys) {
                if ([[dicSub objectForKey:sub] isKindOfClass:[NSDictionary class]]) {
                    NSMutableDictionary *dicSubTwo = [[NSMutableDictionary alloc] initWithDictionary:[dicSub objectForKey:sub]];
                    for (NSString *sub in dicSubTwo.allKeys) {
                        if ([[dicSubTwo objectForKey:sub] isEqual:[NSNull null]]) {
                            [dicSubTwo setObject:@"" forKey:sub];
                        }
                    }
                    [dicSub setObject:dicSubTwo forKey:sub];
                }else  if ([[dicSub objectForKey:sub] isKindOfClass:[NSArray class]]) {
                    NSMutableArray *aryDub = [[NSMutableArray alloc] initWithArray:[dicSub objectForKey:sub]];
                    for (int i=0; i < aryDub.count; i++) {
                        id sub  = aryDub[i];
                        if ([sub isKindOfClass:[NSDictionary class]]) {
                            NSMutableDictionary *dicSub = [[NSMutableDictionary alloc] initWithDictionary:aryDub[i]];
                            [aryDub replaceObjectAtIndex:i withObject:[dicSub deleteAllNullValue]];
                        }
                    }
                    [dicSub setObject:aryDub forKey:sub];
                }
                if ([[dicSub objectForKey:sub] isEqual:[NSNull null]]) {
                    [dicSub setObject:@"" forKey:sub];
                }
            }
            [mutableDic setObject:dicSub forKey:keyStr];
        }else if([[self objectForKey:keyStr] isKindOfClass:[NSArray class]]){
            NSMutableArray *aryDub = [[NSMutableArray alloc] initWithArray:[self objectForKey:keyStr]];
            for (int i=0; i < aryDub.count; i++) {
                id sub  = aryDub[i];
                if ([sub isKindOfClass:[NSDictionary class]]) {
                    NSMutableDictionary *dicSub = [[NSMutableDictionary alloc] initWithDictionary:aryDub[i]];
                    [aryDub replaceObjectAtIndex:i withObject:[dicSub deleteAllNullValue]];
                }
            }
            [mutableDic setObject:aryDub forKey:keyStr];
            
        }else{
            if ([[self objectForKey:keyStr] isEqual:[NSNull null]]) {
                [mutableDic setObject:@"" forKey:keyStr];
            }
            else{
                [mutableDic setObject:[self objectForKey:keyStr] forKey:keyStr];
            }
        }

    }
    return mutableDic;
}
 


@end

 

你可能感兴趣的:(iOS-JSON空值处理)