【发布时间】:2012-10-14 00:05:15
【问题描述】:
我目前正在一个 iOS 应用程序中解析来自 Twitter 的推文。我的应用程序目前可以使用search.twitter.com/search.json?... API 调用执行搜索,并且我的应用程序可以解析这些推文并为每条推文选择我想要的所有数据。我现在正处于尝试获取用户时间线的阶段,但我似乎无法找到一种稳定的方法来解析从用户的时间线 API 调用返回的对象。
使用搜索功能(示例返回的对象位于:https://dev.twitter.com/docs/api/1/get/search 的底部),我可以通过选择 valueForKey:(@"results") 进行解析并像这样遍历所有这些结果:
//jSONText is the returned result from the API in NSDictionary format
NSArray *resultTweetsJSON = [jSONText valueForKey:(@"results")];
for (int tweetIndex=0; tweetIndex<[resultTweetsJSON count]; tweetIndex++) {
//Loop through and pull out raw tweet content for a specific tweet
NSString *rawTweetContent = [resultTweetsJSON objectAtIndex:tweetIndex];
//Build a custom tweet object using that content
Tweet *singleTweet = [[Tweet alloc] initWithResultsContent:rawTweetContent];
}
我希望我可以执行相同或相似的事情,但我似乎找不到一个好的键来使用时间轴返回的数据(例如,底部附近的返回数据:https://dev.twitter.com/docs/api/1/get/statuses/user_timeline)。
如您所见,user_time 结果集不包含我可以从中查询的漂亮的“results”键。
如何轻松地选择和解析来自Get/Statuses/User_Timeline Twitter API 调用的数据并将其转换为 NSArray 以便我可以循环并提取推文?网上的一切似乎都使用了旧技术。我找到了this tutorial,但它似乎将来自 Twitter 的响应作为 NSData 对象而不是 NSDictionary 对象,我相信 NSDictionary 是最新的方法。
这是我认为可能起作用的东西:
//No valueForKey to use, so perhaps use ""?
NSArray *timeline = [jSONText valueForKey:(@"")];
for (int i=0; i<[timeline count]; i++) {
//Looping through each "object" of the timeline, grab the tweet content then fill user content
//Grab the "user" object from the timeline object we're looking at
NSString *rawUserContent = [[timeline objectAtIndex:i] valueForKey:(@"user")];
//NSString *rawUserContent = [timeline valueForKey:(@"user")]; - Maybe this?
//Do whatever else I need to do here...
}
【问题讨论】:
标签: objective-c ios twitter ios6