Created
June 26, 2014 14:52
-
-
Save interchen/52413dbd0ff1cd65a256 to your computer and use it in GitHub Desktop.
Delete NSNull object in Json object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 把response中的NSNull对象删除掉 | |
* | |
* @param response 要处理的对象 | |
* | |
* @return 删除掉NSNull后的新对象 | |
*/ | |
- (id)convertNullObject:(id)response | |
{ | |
if (response == nil || [response isKindOfClass:[NSNull class]]) { | |
return nil; | |
} | |
if ([response isKindOfClass:[NSArray class]]) { | |
NSMutableArray *tempResponse = [NSMutableArray array]; | |
for (id item in response) { | |
id newItem = [self convertNullObject:item]; | |
if (newItem) { | |
[tempResponse addObject:newItem]; | |
} | |
} | |
if (tempResponse.count > 0) { | |
return tempResponse; | |
} else { | |
return nil; | |
} | |
} else if ([response isKindOfClass:[NSDictionary class]]) { | |
NSMutableDictionary *tempResponse = [NSMutableDictionary dictionary]; | |
for (id key in [response allKeys]) { | |
id value = [response objectForKey:key]; | |
id newValue = [self convertNullObject:value]; | |
if (!newValue) { | |
continue; | |
} | |
[tempResponse setObject:newValue forKey:key]; | |
} | |
if (tempResponse.count > 0) { | |
return tempResponse; | |
} else { | |
return nil; | |
} | |
} else { | |
return response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment