检查 JSON 文件中是不是存在数组 - iOS

Posted

技术标签:

【中文标题】检查 JSON 文件中是不是存在数组 - iOS【英文标题】:Check if Array exists in JSON file - iOS检查 JSON 文件中是否存在数组 - iOS 【发布时间】:2013-10-27 16:48:41 【问题描述】:

我有一个 ios 应用程序,它解析 Google Plus 提供的 JSON 提要。此 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"
     
    ]
   
  ,

感谢您抽出宝贵时间,丹。

【问题讨论】:

您遇到了什么错误? 【参考方案1】:

更新:

只需将其分配给数组并检查array 是否为nil

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

这会起作用。

【讨论】:

[NSNull null] 是单例,不需要isEqual: 天啊..我只是忘记了..谢谢@Kevin。 @kevin 我有时会作弊并使用 == 但 API 真的能保证吗? @nielsbot 您不应该使用 == 来比较客观对象。这就是想法。如果我错了,请分享您的想法。 您可以使用== 来比较对象指针。 (即“这些是完全相同的实例吗?”)isEqual: 是根据它们的内容来确定两个对象是否等价。【参考方案2】:

我没有发现问题(请发布您的错误详细信息),但在处理 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

【讨论】:

以上是关于检查 JSON 文件中是不是存在数组 - iOS的主要内容,如果未能解决你的问题,请参考以下文章

如何检查 JSON 对象数组中是不是存在键

检查 JSON 关联数组中是不是存在值

如何使用jq检查数组中是不是存在元素

检查 JSON 密钥是不是存在

如何检查数组中是不是存在相同的键?

检查目录中是不是存在 JSON 文件 - Swift