How can I check If I got null from json?

后端 未结 5 976
孤独总比滥情好
孤独总比滥情好 2020-12-31 09:13

Here I got from JSON

[{\"photo\":null}]

and I use this code

NSMutableArray *jPhoto = [NSMutableArray arrayWithArray:(NSArray *)[jsonDict va         


        
5条回答
  •  自闭症患者
    2020-12-31 09:38

    Hope so this helps.

    -(NSMutableDictionary *)jsonCheckforNull:(NSMutableDictionary *)json{

    NSMutableDictionary* strongjson=[json mutableCopy];

    for (NSString *ktr in json) {
    
        NSObject *str=[json objectForKey:ktr];
    
        if ([str isKindOfClass:[NSArray class]]) {
            if (!(str==[NSNull null])) {
                NSArray *temp = [json allKeysForObject:str];
                str=[[self ArrayCheckforNull:(NSMutableArray*)str]mutableCopy];
                NSString *key = [temp objectAtIndex:0];
                [strongjson removeObjectForKey:key];
                [strongjson setObject:str forKey:key];
            }
            else
            {
                NSArray *temp = [strongjson allKeysForObject:str];
                NSString *key = [temp objectAtIndex:0];
                [strongjson removeObjectForKey:key];
                [strongjson setObject:@"-----" forKey:key];
            }
    
        }
        else if ([str isKindOfClass:[NSDictionary class]]) {
            if (!(str==[NSNull null])) {
                str=[[self jsonCheckforNull:str]mutableCopy];
                NSArray *temp = [strongjson allKeysForObject:str];
                NSString *key = [temp objectAtIndex:0];
                [strongjson removeObjectForKey:key];
                [strongjson setObject:str forKey:key];
            }
            else
            {
                NSArray *temp = [strongjson allKeysForObject:str];
                NSString *key = [temp objectAtIndex:0];
                [strongjson removeObjectForKey:key];
                [strongjson setObject:@"-----" forKey:key];
            }
    
        }
    
        else {
    
            if (str ==[NSNull null]) {
                NSArray *temp = [strongjson allKeysForObject:str];
                NSString *key = [temp objectAtIndex:0];
                [strongjson removeObjectForKey:key];
                [strongjson setObject:@"----" forKey:key];
            }
    
        }
    
    }
    return strongjson;
    

    }

    -(NSMutableArray *)ArrayCheckforNull:(NSMutableArray *)arr{

    NSObject *str;
    NSMutableArray* strongArray=[[[NSMutableArray alloc]initWithArray:arr]mutableCopy];
    for (str in arr)
    {
    
        if ([str isKindOfClass:[NSArray class]]) {
            if (!(str==[NSNull null])) {
                str=[[self ArrayCheckforNull:(NSMutableArray *)str]mutableCopy];
                [strongArray removeObjectAtIndex:0];
                [strongArray addObject:str];
            }
            else
            {
                [strongArray removeObject:str];
                [strongArray addObject:@"----"];
    
            }
    
        }
        else if ([str isKindOfClass:[NSDictionary class]]) {
            if (!(str==[NSNull null])) {
                str=[[self jsonCheckforNull:(NSMutableDictionary*)str]mutableCopy];
                [strongArray removeObjectAtIndex:0];
                [strongArray addObject:str];
    
            }
            else
            {
                [strongArray removeObject:str];
                [strongArray addObject:@"----"];
            }
    
        }
    
        else {
    
            if (str ==[NSNull null]) {
                [strongArray removeObject:str];
                [strongArray addObject:@"----"];
            }
    
    
        }
    
    }
    return strongArray;
    

    }

提交回复
热议问题