开发者

How can I check If I got null from json?

Here I got from JSON

[{"photo":null}]

and I use this code

NSMutableArray *jPhoto = [NSMutableArray arrayWithArray:(NSArray *)[jsonDict valueForKey:@"photo"]];

How can I check it if I want to use if() ??

edit

here is JSON Data

   [{"photo":
              [{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1304928459.jpg","title":"test picture","content":"this开发者_开发技巧 is description for test picture.\r\n\u8aac\u660e\u6587\u306a\u306e\u306b\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb"}
              ,{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1304928115.jpg","title":"nothing","content":"iMirai"}
              ,{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1303276769.jpg","title":"iMirai","content":"Staff"}]}
  ]

and here is my JSON parser

NSError *theError = nil;     
    NSString *URL = [NSString stringWithFormat:@"http://www.yohyeh.com/apps/get_sub_detail.php?id=%@&menu=photo",g_id];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]];
    NSURLResponse *theResponse =[[[NSURLResponse alloc]init] autorelease];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];   
    NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSDictionary *jsonDict = [string JSONValue];

Thank for help


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;
        …
    }
}


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


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜