检查JSON文件中是否存在Array - iOS

时间:2013-10-27 16:48:42

标签: ios json cocoa-touch object nsarray

我有一个iOS应用程序,用于解析Google Plus提供的JSON提要。此JSON Feed包含Google Plus个人资料中帖子的链接,标题等。

某些部分的JSON文件有一个名为“attachments”的数组,该数组包含我正在解析的图像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"
     }
    ]
   }
  },

谢谢你的时间,Dan。

2 个答案:

答案 0 :(得分:1)

<强>更新

只需将其分配给数组并检查arraynil

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

这样可行。

答案 1 :(得分: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