0

我收到了这个 json 响应

{
   "generated_in" = "0.0283";
    stat = ok;
videos =     {
    "on_this_page" = 3;
    page = 1;
    perpage = 50;
    total = 3;
    video =         (
                    {
            "embed_privacy" = anywhere;
            id = 73198189;
            "is_hd" = 1;
            "is_watchlater" = 0;
            license = 0;
            "modified_date" = "2013-08-27 01:29:16";
            owner = 20303618;
            privacy = anybody;
            title = Untitled;
            "upload_date" = "2013-08-27 00:57:36";
        },
                    {
            "embed_privacy" = anywhere;
            id = 73197989;
            "is_hd" = 0;
            "is_watchlater" = 0;
            license = 0;
            "modified_date" = "2013-08-27 01:24:17";
            owner = 20303618;
            privacy = anybody;
            title = sample2;
            "upload_date" = "2013-08-27 00:52:40";
        },
                    {
            "embed_privacy" = anywhere;
            id = 72961770;
            "is_hd" = 0;
            "is_watchlater" = 0;
            license = 0;
            "modified_date" = "2013-08-23 05:57:48";
            owner = 20303618;
            privacy = anybody;
            title = sample;
            "upload_date" = "2013-08-23 05:25:44";
        }
    );
};
}

当我试图解析它的视频ID时。我使用的技术是将 json 转换为 NSDictionary jsondata 和

NSString *videoid = [[[jsondata      objectForKey:@"videos"]valueForKey:@"video"]valueForKey:@"id"];
NSLog(@"video string is %@",videoid);

结果是:(73198189、73197989、72961770)

但我无法访问该字符串上的普通字符串函数来检索 id。它是说发送了一个错误无法识别的选择器。有没有更好的方法来解析那个字符串,我试过谷歌,但每篇文章都说同样的方法。

4

1 回答 1

0

您得到的不是字符串而是字符串数组,JSON 响应包含多个视频。您使用的代码将检索所有id所有视频。

您可以循环播放找到的视频,例如:

NSArray *videoIdArray = [jsondata valueForKeyPath:@"videos.video.id"];

for(NSString *videoId in videoIdArray) {
   NSLog(@"video string is %@",videoId);
}
于 2013-08-27T09:50:51.790 回答