0

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

某些部分的 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"
     }
    ]
   }
  },

谢谢你的时间,丹。

4

2 回答 2

1

更新:

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

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

这将起作用。

于 2013-10-27T17:16:09.953 回答
0

我没有看到问题(请发布您的错误详细信息),但是在处理 JSON 时,我喜欢使用 category 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
于 2013-10-27T17:35:25.880 回答