【问题标题】:Parse Twitter Get Statuses Objective C解析 Twitter 获取状态目标 C
【发布时间】: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


    【解决方案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 返回的结果,我需要做的就是将字典转换为数组。

    【讨论】:

    • 感谢您回答自己的问题 :)
    猜你喜欢
    • 2012-10-23
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2011-09-19
    相关资源
    最近更新 更多