2013-10-27 72 views
0

我有一個iOS應用程序,用於解析Google Plus提供的JSON訂閱源。此JSON供稿包含Google Plus個人資料上帖子的鏈接,標題等。檢查JSON文件中是否存在數組 - iOS

某些部分的JSON文件有一個名爲「附件」的數組,該數組具有我正在解析的圖像URL。

問題是,該數組並不總是存在,所以有時它的圖像URL可用,有時它甚至不存在。

由於這個原因,當我解析圖像URL時,我的iOS應用程序崩潰,因爲它所尋找的數組並不總是存在。

因此,在決定解析圖像URL之前,如何測試數組是否存在。我試圖做一個簡單的if語句,但它似乎不起作用。那就是:

if ([[[episodes objectAtIndex:index] valueForKey:@"object"] valueForKey:@"attachments"] == [NSNull null]) { 
      NSLog("No image to parse."); 
     } 

else { 
    // parse image link 
} 

這裏是JSON文件:

{ 


"kind": "plus#activity", 
    "etag": "\"9Q4kEgczRt3gWehMocqSxXqUhYo/uQcxIDsySGhlI5hMFHcxjuBhM9k\"", 
    "title": "", 
    "published": "2013-02-24T22:30:25.159Z", 
    "updated": "2013-02-24T22:30:25.159Z", 
    "id": "z13jgdihsvnytdttc23hxdz5ypfsjnzsb", 
    "url": "https://plus.google.com/102757352146004417544/posts/7psQLdwS2F7", 
    "actor": { 
    "id": "102757352146004417544", 
    "displayName": "Daniel Sadjadian", 
    "url": "https://plus.google.com/102757352146004417544", 
    "image": { 
    "url": "https://lh4.googleusercontent.com/-EF0LibpIsEY/AAAAAAAAAAI/AAAAAAAAALQ/2nt32bqYBtM/photo.jpg?sz=50" 
    } 
    }, 
    "verb": "post", 
    "object": { 
    "objectType": "note", 
    "content": "", 
    "url": "https://plus.google.com/102757352146004417544/posts/7psQLdwS2F7", 
    "replies": { 
    "totalItems": 0, 
    "selfLink": "https://www.googleapis.com/plus/v1/activities/z13jgdihsvnytdttc23hxdz5ypfsjnzsb/comments" 
    }, 
    "plusoners": { 
    "totalItems": 0, 
    "selfLink": "https://www.googleapis.com/plus/v1/activities/z13jgdihsvnytdttc23hxdz5ypfsjnzsb/people/plusoners" 
    }, 
    "resharers": { 
    "totalItems": 0, 
    "selfLink": "https://www.googleapis.com/plus/v1/activities/z13jgdihsvnytdttc23hxdz5ypfsjnzsb/people/resharers" 
    }, 
    "attachments": [ 
    { 
     "objectType": "video", 
     "displayName": "SGU - The Game Has Changed!", 
     "content": "Send this video to your friends to spread the word about the upcoming SyFy marathon. This very well may be our last chance to save the show. If you're in the...", 
     "url": "http://www.youtube.com/watch?v=WtHusm7Yzd4", 
     "image": { 
     "url": "https://lh3.googleusercontent.com/proxy/z9shhK2d39jM2P-AOLMkqP2KMG2DjipCWlcPeVPaRvHkfj-nmxkxqZfntAVMoV88ZOuroRHIvG4qs-K0SaMjQw=w506-h284-n", 
     "type": "image/jpeg", 
     "height": 284, 
     "width": 506 
     }, 
     "embed": { 
     "url": "http://www.youtube.com/v/WtHusm7Yzd4?version=3&autohide=1", 
     "type": "application/x-shockwave-flash" 
     } 
    } 
    ] 
    }, 
    "provider": { 
    "title": "Google+" 
    }, 
    "access": { 
    "kind": "plus#acl", 
    "description": "Public", 
    "items": [ 
    { 
     "type": "public" 
    } 
    ] 
    } 
    }, 

感謝您的時間,丹。

+0

什麼是你所得到的錯誤類別? – nielsbot

回答

1

UPDATE:

只是賦值給數組,並檢查arraynil

NSMutableArray *attachments = [[[episodes objectAtIndex:index] valueForKey:@"object"] valueForKey:@"attachments"]; 
if (!attachments) { 
      NSLog("No image to parse."); 
     } 

這將工作。

+0

'[NSNull null]'是一個單例,不需要'isEqual:'。 – Kevin

+0

OMG ..我只是忘了那個..謝謝@Kevin。 –

+0

@kevin我有時會欺騙和使用==,但實際上是由API保證的嗎? – nielsbot

0

我沒有看到問題(請張貼您的錯誤詳情),但是在處理JSON時,我喜歡使用IfNullThenNil類別。你的代碼是這樣:

if ([ [ episodes[index] valueForKeyPath:@"object.attachments" ] ifNullThenNil ] 
{ 
    // parse image link 
} 
else 
{ 
    NSLog(@"No image to parse.\n"); 
} 

這裏有NSObject/NSNull

@implementation NSObject (IfNullThenNil) 
-(id)ifNullThenNil 
{ 
    return self ; 
} 
@end 

@implementation NSNull (IfNullThenNil) 
-(id)ifNullThenNil 
{ 
    return nil ; 
} 
@end 
+0

+1誰給了這個downvote? –

+0

我討厭SO的匿名投票。至少說出爲什麼...... – nielsbot