1

現在、iOS アプリケーションで Twitter からのツイートを解析しています。現在、私のアプリは API 呼び出しを使用して検索を実行できsearch.twitter.com/search.json?...、アプリはこれらのツイートを解析して、各ツイートに必要なすべてのデータを選択できます。現在、ユーザーのタイムラインを取得しようとしている段階ですが、ユーザーのタイムライン API 呼び出しから返されたオブジェクトを解析する安定した方法が見つからないようです。

検索機能 ( https://dev.twitter.com/docs/api/1/get/searchvalueForKey:(@"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

Twitter APIの呼び出しからデータを簡単に選択して解析し、Get/Statuses/User_Timelineそれを NSArray に変換して、ツイートをループして抽出できるようにするにはどうすればよいでしょうか? オンラインのすべてが古い手法を使用しているようです。このチュートリアルを見つけましたが、Twitter からの応答を NSDictionary オブジェクトではなく NSData オブジェクトとして受け取っているようです。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...

    }
4

1 に答える 1

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-29T19:00:55.497 に答える