2014-01-22 33 views

回答

1

您通過Dictionary的鑰匙需要iterate值:

嘗試類似:

NSArray *keys= [json allKeys]; 
     for (NSString *keysV in keys){ 
      NSLog(@"Keys are %@", keysV); 
     if([[Your_Dict objectForKey: keysV] isEqual:[NSNull null]]){ 
     //Do your stuff here 
     } 
    } 

編輯:我沒有測試。

NSArray *keys= [json allKeys]; 
    for (NSString *keysV in keys){ 
     for(NSDictionary *subDict in [yourDict objectForKey: keysV]){ 
      NSArray *subKeys = [subDict allKeys]; 
      for (NSString *keysVv in subKeys){ 
       if([[subDict objectForKey: keysVv] isEqual:[NSNull null]]){ 
        //Do your stuff here 
       } 
      } 
     } 
} 
+0

感謝,但它遍歷1LV DIC如{人,類} 我想去一個級別深度 – Peerax

+0

然後重複相同的另一個循環。意思是,在字典中迭代。 –

+0

嘗試類似這個,\ NSArray * keys = [json allKeys]; for(NSString * keysV in keys){for(NSDictionary * subDict in [yourDict objectForKey:keysV]){NSArray * subKeys = [subDict allKeys];對於(NSString * keysVv in subKeys){ if([subDict objectForKey:keysVv] isEqual:[NSNull null]]){ //在這裏做你的東西 }}} –

0

與我做了同樣的問題,我可以解決由數據轉換NSMutableDicrionary,我不斷地檢查數據若有空數據來我更換了與空字符串。 嘗試它可能會幫助你。

-(void)ConfigureCell:(int)Section :(int)Index :(NSMutableArray *)threadArray{ 
NSMutableDictionary *threadDict=[threadArray objectAtIndex:Index]; 

    if([[threadDict objectForKey:@"read"]boolValue]){ 

    } 
    CGRect subjectLabelSize; 
    [email protected]"1323123123123"; 
    self.lastMessageLabel.text=[threadDict objectForKey:@"lastMessage"]; 
    if ([[threadDict objectForKey:@"subject"] isEqual:[NSNull null]]) { 
     [email protected]""; 
    } 
} 
+0

這種方法是蠻力。 我試試這個,但不便在我的應用程序。 我的應用程序必須從服務器那麼多的數據。 – Peerax

2

創造生命時間範疇更換空

創建類別(換上黑色的空間空字符串)

的NSDictionary + NullReplacement.h

#import <Foundation/Foundation.h> 
@interface NSDictionary (NullReplacement) 
- (NSDictionary *)dictionaryByReplacingNullsWithBlanks; 
@end 

的NSDictionary + NullReplacement .M

#import "NSDictionary+NullReplacement.h" 
#import "NSArray+NullReplacement.h" 

@implementation NSDictionary (NullReplacement) 

- (NSDictionary *)dictionaryByReplacingNullsWithBlanks { 
    const NSMutableDictionary *replaced = [self mutableCopy]; 
    const id nul = [NSNull null]; 
    const NSString *blank = @""; 

    for (NSString *key in self) { 
     id object = [self objectForKey:key]; 
     if (object == nul) [replaced setObject:blank forKey:key]; 
     else if ([object isKindOfClass:[NSDictionary class]]) [replaced setObject:[object dictionaryByReplacingNullsWithBlanks] forKey:key]; 
     else if ([object isKindOfClass:[NSArray class]]) [replaced setObject:[object arrayByReplacingNullsWithBlanks] forKey:key]; 
    } 
    return [NSDictionary dictionaryWithDictionary:[replaced copy]]; 
} 

@end 

再創建一個類別(換上黑色的空間空數組)

的NSArray + NullReplacement.h

#import <Foundation/Foundation.h> 
@interface NSArray (NullReplacement) 
- (NSArray *)arrayByReplacingNullsWithBlanks; 
@end 

的NSArray + NullReplacement.m

#import "NSArray+NullReplacement.h" 
#import "NSDictionary+NullReplacement.h" 

@implementation NSArray (NullReplacement) 

- (NSArray *)arrayByReplacingNullsWithBlanks { 
    NSMutableArray *replaced = [self mutableCopy]; 
    const id nul = [NSNull null]; 
    const NSString *blank = @""; 
    for (int idx = 0; idx < [replaced count]; idx++) { 
     id object = [replaced objectAtIndex:idx]; 
     if (object == nul) [replaced replaceObjectAtIndex:idx withObject:blank]; 
     else if ([object isKindOfClass:[NSDictionary class]]) [replaced replaceObjectAtIndex:idx withObject:[object dictionaryByReplacingNullsWithBlanks]]; 
     else if ([object isKindOfClass:[NSArray class]]) [replaced replaceObjectAtIndex:idx withObject:[object arrayByReplacingNullsWithBlanks]]; 
    } 
    return [replaced copy]; 
} 

@end 

需要使用單一線路和享受無空字典

#import "NSDictionary+NullReplacement.h" 

NSDictionary * withoutNullResultDict=[resultDict dictionaryByReplacingNullsWithBlanks]; 
0

使用下面的代碼:

for (NSDictionary * test in Your_Array) 
      { 
       for (id dict in [test allKeys]) 
       { 
        ([test valueForKey:dict] == [NSNull null]) ? [test setValue:@"" forKey:dict] :[test setValue:[test valueForKey:dict] forKey:dict]; 
       } 
      }