0

我正在尝试为 YouTube API 提取 GData 提供的字符串的一部分。我可以分离出视频标题和缩略图字符串,但需要创建一个文本描述标签。我已经复制了下面的完整字符串,但我遇到的困难是从中提取以下部分。

我被困在如何通过type:plain content复制后面的文本然后在}分隔符上结束新字符串来创建新的 NSString 。

{type:plain content: 整整一周我几乎每天都制作一个视频,现在我感觉不像以前那么紧张了。结束!

汉克(和他的兄弟约翰):http: //youtube.com/vlogbrothers }

我曾尝试使用扫描仪但没有成功,我们将不胜感激有关此问题的任何建议或指示。

完整字符串的完整格式如下。

GDataEntryYouTubeVideo 0xc125e20: {v:2.1 title:Crisis Averted contentSrc:https://www.youtube.com/v/RKQWfVzUBt8?version=3&f=user_uploads&app=youtube_gdata etag:W/"DUEGR347eCp7I2A9WhVVEEo." authors:1 categories:12 links:alternate,video.responses,video.related,mobile,self id:tag:youtube.com,2008:video:RKQWfVzUBt8 rating:+19935/-228 comment:GDataComment 0xc12c260: {feedLink:GDataFeedLink 0xc12c9c0: {href:https://gdata.youtube.com/feeds/api/videos/RKQWfVzUBt8/comments rel:http://gdata.youtube.com/schemas/2007#comments countHint:7096 href:https://gdata.youtube.com/feeds/api/videos/RKQWfVzUBt8/comments countHint:7096 rel:http://gdata.youtube.com/schemas/2007#comments}} stats:GDataYouTubeStatistics 0xc12d640: {viewCount:810340 favoriteCount:1096} mediaGroup:GDataYouTubeMediaGroup 0xc12e700: {categories:(
    "GDataMediaCategory 0xc131ab0: {label:People & Blogs scheme:http://gdata.youtube.com/schemas/2007/categories.cat content:People}" ) contents:(
    "GDataMediaContent 0xc130750: {url:https://www.youtube.com/v/RKQWfVzUBt8?version=3&f=user_uploads&app=youtube_gdata type:application/x-shockwave-flash medium:video isDefault:true expression:full duration:201}",
    "GDataMediaContent 0xc130f80: {url:rtsp://v5.cache2.c.youtube.com/CigLENy73wIaHwnfBtRcfRakRBMYDSANFEgGUgx1c2VyX3VwbG9hZHMM/0/0/0/video.3gp type:video/3gpp medium:video expression:full duration:201}",
    "GDataMediaContent 0xc131530: {url:rtsp://v2.cache2.c.youtube.com/CigLENy73wIaHwnfBtRcfRakRBMYESARFEgGUgx1c2VyX3VwbG9hZHMM/0/0/0/video.3gp type:video/3gpp medium:video expression:full duration:201}" ) credits:(
    "GDataMediaCredit 0xc131ff0: {role:uploader scheme:urn:youtube content:charlieissocoollike unparsedAttr:yt:display}" ) thumbnails:(
    "GDataMediaThumbnail 0xc132c10: {url:http://i.ytimg.com/vi/RKQWfVzUBt8/default.jpg height:90 width:120 time:00:01:40.500}",
    "GDataMediaThumbnail 0xc1336e0: {url:http://i.ytimg.com/vi/RKQWfVzUBt8/mqdefault.jpg height:180 width:320}",
    "GDataMediaThumbnail 0xc133bb0: {url:http://i.ytimg.com/vi/RKQWfVzUBt8/hqdefault.jpg height:360 width:480}",
    "GDataMediaThumbnail 0xc133f40: {url:http://i.ytimg.com/vi/RKQWfVzUBt8/1.jpg height:90 width:120 time:00:00:50.250}",
    "GDataMediaThumbnail 0xc134320: {url:http://i.ytimg.com/vi/RKQWfVzUBt8/2.jpg height:90 width:120 time:00:01:40.500}",
    "GDataMediaThumbnail 0xc134630: {url:http://i.ytimg.com/vi/RKQWfVzUBt8/3.jpg height:90 width:120 time:00:02:30.750}" ) keywords:GDataMediaKeywords 0xc134b70: {content:Charlie McDonnell, charlieissocoollike, YouTube, Mid-Life, Crisis, Week, Averted, Hank Green, vlogbrothers, America keywords:Charlie McDonnell, charlieissocoollike, YouTube, Mid-Life, Crisis, Week, Averted, Hank Green, vlogbrothers, America} description:GDataMediaDescription 0xc132880: {type:plain content:I made a video pretty much every day for a whole week, and now I'm not feeling quite as stressed out as I was previously. The end!

Hank (and his brother John): http://youtube.com/vlogbrothers} players:(
    "GDataMediaPlayer 0xc130960: {url:https://www.youtube.com/watch?v=RKQWfVzUBt8&feature=youtube_gdata_player}"
4

2 回答 2

0

您的字符串可能看起来像 JSON,但它不是有效的 JSON。完成您所要求的一种方法是使用正则表达式 (iOS4+)。您可以尝试这样的事情(假设这myStringNSString您提供的文本......):

NSError *error;
NSRegularExpression *regex = [NSRegularExpression 
                              regularExpressionWithPattern:@"\\{type:plain.+\\}(?=\\s)"
                              options:NSRegularExpressionDotMatchesLineSeparators
                              error:&error];

NSRange range   = [regex rangeOfFirstMatchInString:myString
                                           options:0 
                                             range:NSMakeRange(0, [myString length])];
NSString *result = [myString substringWithRange:range];

result现在是:

{type:plain content:I made a video pretty much every day for a whole week, and now I'm not feeling quite as stressed out as I was previously. The end!

Hank (and his brother John): http://youtube.com/vlogbrothers}

我并不是说这是最好的方法,我同意如果你有一个有效的 JSON 字符串,反序列化(正如 Kendall 建议的)将是最佳的。但至少可以完成这项工作......

于 2012-05-03T21:59:08.797 回答
0

这看起来像 JSON,如果您只针对 iOS5,您可以使用内置的 JSON 转换器将该 JSON 转换为 NSDictionary,您可以在其中轻松找到所需的数据。

于 2012-05-03T20:57:41.683 回答