2012-10-23 57 views
1

我目前正致力于在iOS应用程序中解析来自Twitter的推文。我的应用程序目前可以使用search.twitter.com/search.json?... API调用执行搜索,我的应用程序可以解析这些推文,并为每条推文选择所需的所有数据。我处于现在正在尝试获取用户时间轴的阶段,但似乎无法找到稳定的方式来解析用户时间轴API调用中返回的对象。解析Twitter获取状态目标C

随着搜索功能(例如返回在底部对象: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... 

    } 

回答

1

几天后,我发现我只是过于复杂的问题。我需要做的就是转换为一个数组并进行相应的解析。

//The above code was taken out for brevity. 
    //userTimeline = the returned result from the API call to Get User Timeline 
    NSDictionary *userTimeline = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError]; 

    //convert the NSDictionary to an array 
    NSArray *timeline = (NSArray*)userTimeline; 

    //loop through the timeline array object 
    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" section of the tweet we're looking at 
     NSString *rawUserContent = [[timeline objectAtIndex:i] valueForKey:(@"user")]; 

     //Fill singleTweet with user content 
     //initWithUserContent is a custom method I wrote to parse apart the User data 
     //Tweet is a custom class I wrote to hold data about a particular tweet 
     Tweet *singleTweet = [[Tweet alloc] initWithUserContent:rawUserContent]; 

     //At this point singleTweet will be filled with user content, so now fill with tweet content (text and created_at for now) 
     singleTweet.text = [[timeline objectAtIndex:i] valueForKey:(@"text")]; 
     singleTweet.created_at = [[timeline objectAtIndex:i] valueForKey:(@"created_at")]; 

然后我把所有上面的代码包装在if语句中。如果用户正在执行搜索(使用Twitter的搜索API),则执行上述问题中的所有代码,如果用户正在执行用户时间轴中的选择,则执行此答案中的所有代码。

工程就像魅力,我现在可以正确解析两个JSON返回的结果,我需要做的就是将字典转换为数组。

+0

谢谢你回答你自己的问题:) –