用JSON响应中的空字符串替换空值在iOS中不起作用 [英] Replacing nulls with empty string from JSON response not working in iOS

查看:102
本文介绍了用JSON响应中的空字符串替换空值在iOS中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码用空字符串替换json响应中的空值,但是它不起作用.请帮我解决此问题,在很多情况下和应用程序崩溃时,服务器响应都为空.

I used below code to replace nulls from json response with empty string, but it is not working. Please help me out to fix this issue, i am getting nulls from server response in lot of scenarios and App crashes.

代码:

- (NSMutableDictionary *)recursive:(NSMutableDictionary *)dictionaryResponse {

    NSMutableDictionary *dictionary=[[NSMutableDictionary alloc]initWithDictionary:dictionaryResponse];
    for (NSString *key in [dictionary allKeys]) {
        id nullString = [dictionary objectForKey:key];
        if ([nullString isKindOfClass:[NSDictionary class]]) {
            [self recursive:(NSMutableDictionary*)nullString];
        }else if([nullString isKindOfClass:[NSArray class]]){
            for (int i=0; i<[nullString count]; i++) {
                id nullstr = [nullString objectAtIndex:i];

                if ([nullstr isKindOfClass:[NSDictionary class]]) {
                    [self recursive:(NSMutableDictionary*)nullstr];

                }

            }
        }else {
            if ((NSString*)nullString == (id)[NSNull null])
                [dictionary setValue:@"" forKey:key];
        }
    }
    return dictionary;
}

推荐答案

尝试一下:

- (NSMutableDictionary *)recursiveNullRemove:(NSMutableDictionary *)dictionaryResponse {

    NSMutableDictionary *dictionary = [dictionaryResponse mutableCopy];
    NSString *nullString = @"";
    for (NSString *key in [dictionary allKeys]) {
        id value = dictionary[key];

        if ([value isKindOfClass:[NSDictionary class]]) {

            dictionary[key] = [self recursiveNullRemove:(NSMutableDictionary*)value];  

        }else if([value isKindOfClass:[NSArray class]]){

            NSMutableArray *newArray = [value mutableCopy];
            for (int i = 0; i < [value count]; ++i) {

                id value2 = [value objectAtIndex:i];

                if ([value2 isKindOfClass:[NSDictionary class]]) {
                    newArray[i] = [self recursiveNullRemove:(NSMutableDictionary*)value2];                    
                }
                else if ([value2 isKindOfClass:[NSNull class]]){
                    newArray[i] = nullString;
                }
            }
            dictionary[key] = newArray;
        }else if ([value isKindOfClass:[NSNull class]]){
            dictionary[key] = nullString;
        }
    }
    return dictionary;
}

已编辑

我在答案中发现了问题,现在已经解决

I hase find the problem in my answer, and now it fixed

这是校验码:

NSDictionary *dict = @{@"1" : @{@"1.1": @"value", @"1.2" : [NSNull null]},
                       @"2" : @[@"2.1", @{@"2.2.1" : @"val", @"2.2.2" : [NSNull null]}],
                       @"3" : [NSNull null]};

dict = [self recursiveNullRemove:dict];

这篇关于用JSON响应中的空字符串替换空值在iOS中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆