當前位置:成語大全網 - 新華字典 - ios 怎麽去掉字典json的null

ios 怎麽去掉字典json的null

通過[x isKindOfClass:[NSNull class]]判斷就可以了如果是null就改成@""

- (NSArray *)checkNSArrayWithChangeUseful:(NSArray *)checkArray

{

if([checkArray isKindOfClass:[NSArray class]])

{

NSMutableArray *usefulArr = [NSMutableArray arrayWithArray:checkArray];

[usefulArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

if([obj isKindOfClass:[NSNull class]] )

{

[usefulArr replaceObjectAtIndex:idx withObject:@""];

}else if([obj isKindOfClass:[NSNumber class]] || [obj isKindOfClass:[NSValue class]])

{

[usefulArr replaceObjectAtIndex:idx withObject:[NSString stringWithFormat:@"%@",obj]];

}else if([obj isKindOfClass:[NSDictionary class]])

{

[usefulArr replaceObjectAtIndex:idx withObject:[self checkNSDictinaryWithChangeUseful:obj]];

}else if([obj isKindOfClass:[NSArray class]])

{

[usefulArr replaceObjectAtIndex:idx withObject:[self checkNSArrayWithChangeUseful:obj]];

}

}];

return [NSArray arrayWithArray:usefulArr];

}

return checkArray;

}

- (NSDictionary *)checkNSDictinaryWithChangeUseful:(NSDictionary *)checkDictioary

{

if([checkDictioary isKindOfClass:[NSDictionary class]])

{

NSMutableDictionary *usefulDic = [NSMutableDictionary dictionaryWithDictionary:checkDictioary];

[usefulDic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {

if(ISNULL(obj) || ISNil(obj))

{

usefulDic[key] = @"";

}else if([obj isKindOfClass:[NSNumber class]] || [obj isKindOfClass:[NSValue class]])

{

usefulDic[key] = [NSString stringWithFormat:@"%@",obj];

}else if([obj isKindOfClass:[NSDictionary class]])

{

usefulDic[key] = [self checkNSDictinaryWithChangeUseful: obj];

}else if([obj isKindOfClass:[NSArray class]])

{

usefulDic[key] = [self checkNSArrayWithChangeUseful:obj];

}

}];

return [NSDictionary dictionaryWithDictionary:usefulDic];

}

return checkDictioary;

}