How can I check If I got null from json?

后端 未结 5 953
孤独总比滥情好
孤独总比滥情好 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:24

    There's no easy way of dealing with this, but the way I do it is to make a category on NSObject:

    @interface NSObject (NotNull)
    
    - (instancetype)notNull;
    
    @end
    

    Implemented like so:

    @implementation NSObject (NotNull)
    
    - (instancetype)notNull
    {
        return self;
    }
    
    @end
    
    @implementation NSNull (NotNull)
    
    - (instancetype)notNull
    {
        return nil;
    }
    
    @end
    

    Then you can send notNull to any optional object in a JSON dict and you'll get nil back if it's NSNull. Otherwise you get the original object. For example:

    self.parentIdentifier = [dictionary[@"parent_id"] notNull];
    
    0 讨论(0)
  • 2020-12-31 09:31

    I believe most JSON parsers represent null as [NSNull null].

    Considering jsonDict points to that single element in the array, then the following should work:

    if ([jsonDict objectForKey:@"photo"] == [NSNull null]) {
        // it's null
    }
    

    Edit based on comment: so jsonDict, despite its name, is an array. In that case, rename jsonDict to jsonArray to avoid further confusion. Then, considering jsonArray points to an array similar to the example posted in the question:

    NSArray *photos = [jsonArray valueForKey:@"photo"];
    for (id photo in photos) {
        if (photo == [NSNull null]) {
            // photo is null
        }
        else {
            // photo isn't null
        }
    }
    

    Further edit based on OP’s modified question:

    NSArray *jsonArray = [string JSONValue];
    
    NSArray *photos = [jsonArray valueForKey:@"photo"];
    for (id photo in photos) {
        if (photo == [NSNull null]) {
            // photo is null
        }
        else {
            // photo isn't null. It's an array
            NSArray *innerPhotos = photo;
            …
        }
    }
    
    0 讨论(0)
  • 2020-12-31 09:35

    try to check for [jPhoto count] or [NSNull null]

    0 讨论(0)
  • 2020-12-31 09:36

    Macros can be helpful if payload is complex JSON structure having possible values.

    #define SET_IF_NOT_NULL(TARGET, VAL) if(VAL != [NSNull null]) { TARGET = VAL; }
    

    and macro can be referenced like

    SET_IF_NOT_NULL(myRecord.name, [jsonData objectForKey:@"name"]);
    
    0 讨论(0)
  • 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;
    

    }

    0 讨论(0)
提交回复
热议问题