IOS,如何在数组中的 dic 中循环 dic 以替换空字符串的空值

Posted

技术标签:

【中文标题】IOS,如何在数组中的 dic 中循环 dic 以替换空字符串的空值【英文标题】:IOS ,How can I loop for dic in dic in array for replace null value for empty string 【发布时间】:2014-01-22 04:21:06 【问题描述】:

从服务器返回的数据中有一个空值

如何通过 NSJSONSerialization 或其他方法将空值替换为空字符串?

我的 JSON 数据是:

 
 person:
       [name:name1,.....,name:name2,....]
      ,
 class:
       [classname:null,.....,classname:classname2,....]
      

感谢您的提前。

【问题讨论】:

【参考方案1】:

创建生命周期类别以替换 Null

创建类别(用空格替换空字符串)

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];

【讨论】:

【参考方案2】:

您需要通过Dictionary iterate 获取值为 null 的键:

尝试类似的:

 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
                
            
        

【讨论】:

谢谢,但它会循环通过 1Lv dic,例如 person,class 我想去一级深度 然后对另一个循环重复相同的操作。意思是,遍历字典。 试试类似的,\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]]) //在这里做你的东西 【参考方案3】:

我也遇到了同样的问题,我可以通过转换NSMutableDicrionary 中的数据来解决这个问题,如果有任何空数据出现,我会不断检查数据,然后用空白字符串替换。 试试看可能对你有帮助。

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

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

    
    CGRect subjectLabelSize;
    self.contactNameLabel.text=@"1323123123123";
    self.lastMessageLabel.text=[threadDict objectForKey:@"lastMessage"];
    if ([[threadDict objectForKey:@"subject"] isEqual:[NSNull null]]) 
        self.subjectLabel.text=@"";
    

【讨论】:

这种方法是蛮力的。我尝试了这个,但在我的应用程序中不便。我的应用程序有很多来自服务器的数据。【参考方案4】:

使用以下代码:

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];
                
            

【讨论】:

以上是关于IOS,如何在数组中的 dic 中循环 dic 以替换空字符串的空值的主要内容,如果未能解决你的问题,请参考以下文章

iOS向数组添加动态值

for循环删除列表里的内容 删除字典中的内容

iOS -- 给model赋值时走了[self setValuesForKeysWithDictionary:dic]不走setvalue: forked:

我应该将依赖注入容器(DIC)作为 MVC 中的参数传递吗?

如何自动对字典中的单词进行分类?

enumerate用法