Json去空

我们在获取网络数据的时候,经常会遇到NSNull的问题,那我们如何去掉这个NsNull呢,

- (NSMutableDictionary *)dictionaryWithOutNull
{
    NSArray *keysArr = [self allKeys];
    for (NSString *keyStr in keysArr)
    {
        id value = [self objectForKey:keyStr];
        if ([value isKindOfClass:[NSNull class]])
            {
               [self setValue:@"" forKey:keyStr];
            }
        else if ([value isKindOfClass:[NSArray class]])
        {
            NSMutableArray *valueArr = [NSMutableArray arrayWithArray:value];
        
        for (id arrElements in value)
        {
            if ([arrElements isKindOfClass:[NSDictionary class]])
            {
                NSMutableDictionary *mutaDic = [NSMutableDictionary dictionaryWithDictionary:arrElements];
                
                NSInteger index = [valueArr indexOfObject:arrElements];
                NSDictionary *subDic = (NSDictionary *)[mutaDic dictionaryWithOutNull];
                [valueArr replaceObjectAtIndex:index withObject:subDic];
                
            }
        }
        [self setValue:valueArr forKey:keyStr];
        //return dic;
    }
    else if ([value isKindOfClass:[NSDictionary class]])
    {
          NSMutableDictionary *mutaDic = [NSMutableDictionary dictionaryWithDictionary:value];
          [self setValue:[mutaDic dictionaryWithOutNull] forKey:keyStr];
        //return dic;
      }
    }
    return self;
}

你可能感兴趣的:(Json去空)