如何检查值null并在NSDictionary中替换它

时间:2012-12-15 04:25:11

标签: ios json nsdictionary

现在,我正在使用NSUserDefault和NSDictionary,我将NSDictionary保存在NSUserDefault中,遗憾的是我不能,因为NSDictionary返回Json的值为null。

我需要检查NSDictionary是否具有null值并替换。怎么样?

这是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>"
       }
      ]
})

1 个答案:

答案 0 :(得分:2)

请你查一下这个链接,我想,这对你有所帮助,谢谢你的链接 Replace occurrences of NSNull in nested NSDictionary

UPDATE:我修改了原始版本,并使用了一些将nsarray转换为nsdictionary的功能来自此链接Convert NSArray to NSDictionary,因为我对您的代码一无所知,所以我尝试一下使我的json字符串尽可能与您的相同,并且它的工作,请参阅以下内容。 :)

- (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 = "";
    };
}

希望它可以帮到你:)。

相关问题