我怎样才能检查,如果我从JSON空?

我从这里得到了JSON

[{ “照片”:空}]

我使用这个代码

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

我如何检查它,如果我想使用if()?

编辑

这里是JSON数据

  [{"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"}]} ] 

这里是我的JSONparsing器

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

感谢您的帮助

我相信大多数JSONparsing器表示为[NSNull null]

考虑到jsonDict指向数组中的单个元素,那么应该如下工作:

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

编辑基于评论:所以jsonDict ,尽pipe它的名字,是一个数组。 在这种情况下,将jsonDict重命名为jsonArray以避免更多混淆。 然后,考虑到jsonArray指向一个类似于问题中发布的例子的数组:

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

根据OP的修改后的问题进一步编辑:

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

如果负载是复杂的JSON结构,macros可能会有帮助。

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

和macros可以参考一样

 SET_IF_NOT_NULL(myRecord.name, [jsonData objectForKey:@"name"]); 

有没有简单的方法来处理这个,但我这样做的方式是在NSObject上做一个类别:

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

像这样执行:

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

然后你可以发送一个JSON字典中的任何可选对象notNull ,如果它是NSNull,你将得到nil 。 否则,你会得到原始的对象。 例如:

 self.parentIdentifier = [dictionary[@"parent_id"] notNull]; 

尝试检查[jPhoto count][NSNull null]

希望所以这有助于。

– (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; 

}