如何检查值null并将其replace在NSDictionary中

现在,我正在使用NSUserDefault和NSDictionary,我保存在NSUserDefault的NSDictionary,不幸的是我不能,因为NSDictionary返回到Json有空值。

我需要检查,如果NSDictionary具有空值和replace。 怎么样?

这是NSDictionary,

({ ID:11, name:21-12-2012, des:"<null>", url: [ { ID:1, name: "<null>" }, { ID:2, name:"<null>" } ] }, { ID:12, name:if i die young, des:"<null>", url: [ { ID:3, name: "<null>" }, { ID:21, name:"<null>" } ] }) 

你可以请检查这个链接,我认为,这将是对你有帮助,感谢这个链接replace嵌套NSDictionary中的NSNull的出现

UPDATE :我修改原来的一点,并使用一些从此链接转换 nsarray到nsdictionary的function转换NSArray到NSDictionary ,因为我不知道任何关于您的代码,所以我尝试使我的jsonstring尽可能接近与你的一样,这是工作,看到以下内容。 🙂

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. tmpDict = [[NSMutableDictionary alloc] init]; NSMutableDictionary *testDict = [[NSMutableDictionary alloc] init]; [testDict setValue:[NSNull null] forKey:@"NullValue"]; [testDict setValue:@"test" forKey:@"UnNull"]; subArr = [[NSMutableArray alloc] initWithObjects:testDict, testDict, nil]; [tmpDict setValue:[NSNull null] forKey:@"NullHere"]; [tmpDict setValue:@"wear" forKey:@"NotNull"]; [tmpDict setObject:subArr forKey:@"Array"]; myArr = [[NSMutableArray alloc] initWithObjects:tmpDict, tmpDict, nil]; NSLog(@"struct: %@", myArr); [self dictionaryByReplacingNullsWithStrings]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void) dictionaryByReplacingNullsWithStrings { const NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary: [self indexKeyedDictionaryFromArray:myArr]]; const id nul = [NSNull null]; const NSString *blank = @""; for (NSString *key in [replaced allKeys]) { const id object = [replaced objectForKey: key]; if (object == nul) { [replaced setObject: blank forKey: key]; } else if ([object isKindOfClass: [NSDictionary class]]) { NSLog(@"found null inside and key is %@", key); [replaced setObject:[self replaceNullInNested:object] forKey:key]; } } NSLog(@"replaced: %@", replaced); } - (NSMutableDictionary *)replaceNullInNested:(NSDictionary *)targetDict { //make it to be NSMutableDictionary in case that it is nsdictionary NSMutableDictionary *m = [targetDict mutableCopy]; NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary: m]; const id nul = [NSNull null]; const NSString *blank = @""; for (NSString *key in [replaced allKeys]) { const id object = [replaced objectForKey: key]; if (object == nul) { [replaced setObject: blank forKey: key]; } else if ([object isKindOfClass: [NSArray class]]) { NSLog(@"found null inside and key is %@", key); //make it to be able to set value by create a new one NSMutableArray *a = [object mutableCopy]; for (int i =0; i< [a count]; i++) { for (NSString *subKey in [[a objectAtIndex:i] allKeys]) { // NSLog(@"key: %@", subKey); // NSLog(@"value: %@", [[object objectAtIndex:i] valueForKey:subKey]); if ([[object objectAtIndex:i] valueForKey:subKey] == nul) { [[object objectAtIndex:i] setValue:blank forKey:subKey]; } } } //replace the updated one with old one [replaced setObject:a forKey:key]; } } return replaced; } - (NSDictionary *) indexKeyedDictionaryFromArray:(NSArray *)array { id objectInstance; NSUInteger indexKey = 0; NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init]; for (objectInstance in array) [mutableDictionary setObject:objectInstance forKey:[NSNumber numberWithUnsignedInt:indexKey++]]; return (NSDictionary *)mutableDictionary; } 

从上面的代码,这是结果:

 replacenullvalue[1590:11303] struct: ( { Array = ( { NullValue = "<null>"; UnNull = test; }, { NullValue = "<null>"; UnNull = test; } ); NotNull = wear; NullHere = "<null>"; }, { Array = ( { NullValue = "<null>"; UnNull = test; }, { NullValue = "<null>"; UnNull = test; } ); NotNull = wear; NullHere = "<null>"; } ) 2012-12-16 15:16:22.790 replacenullvalue[1590:11303] found null inside and key is 0 2012-12-16 15:16:22.790 replacenullvalue[1590:11303] found null inside and key is Array 2012-12-16 15:16:22.791 replacenullvalue[1590:11303] found null inside and key is 1 2012-12-16 15:16:22.791 replacenullvalue[1590:11303] found null inside and key is Array 2012-12-16 15:16:22.792 replacenullvalue[1590:11303] replaced: { 0 = { Array = ( { NullValue = ""; UnNull = test; }, { NullValue = ""; UnNull = test; } ); NotNull = wear; NullHere = ""; }; 1 = { Array = ( { NullValue = ""; UnNull = test; }, { NullValue = ""; UnNull = test; } ); NotNull = wear; NullHere = ""; }; } 

希望它可以帮助你:)。