0

我找到了关于检索 twitter 用户时间线并将它们解析为 tableviews 的很好的教程,但是如果你尝试请求 twitter 搜索,那么解析就会出错。事实是,使用时间轴 NSJSONSerialization 返回一个不错的 NSArray,而使用 twitter 搜索,您会得到一个 NSArray,其值包含所有 json 信息......有没有已知的方法来解析 twitter 搜索结果或者我应该在我自己的?

现在,如果您请求时间表,这就是您所拥有的:

NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/home_timeline.json"];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:@"100" forKey:@"count"];
[parameters setObject:@"1" forKey:@"include_entities"];

JSON响应是:

2012-11-13 20:01:20.725 MicroDebates[1108:1a03] value is: 
{
contributors = "<null>";
coordinates = "<null>";
"created_at" = "Tue Nov 13 18:50:36 +0000 2012";
entities =     {
    hashtags =();
    urls =();
    "user_mentions" =         (
                    {
            id = 171754967;
            "id_str" = 171754967;
            indices =                 (
                3,
                13
            );
            name = "Charles Huang";
            "screen_name" = 1CharlesH;
        },
                    {
            id = 3686561;
            "id_str" = 3686561;
            indices =                 (
                74,
                89
            );
            name = "Jason L. Baptiste";
            "screen_name" = JasonLBaptiste;
        }
    );
};
favorited = 0;
geo = "<null>";
id = 268425649675460608;
"id_str" = 268425649675460608;
"in_reply_to_screen_name" = "<null>";
"in_reply_to_status_id" = "<null>";
"in_reply_to_status_id_str" = "<null>";
"in_reply_to_user_id" = "<null>";
"in_reply_to_user_id_str" = "<null>";
place = "<null>";
"retweet_count" = 1;
retweeted = 0;
"retweeted_status" =     {
    contributors = "<null>";
    coordinates =         {
        coordinates =             (
            "-73.96833024999999",
            "40.75536822"
        );
        type = Point;
    };
    "created_at" = "Tue Nov 13 18:46:30 +0000 2012";
    entities =         {
        hashtags =             (
        );
        urls =             (
                            {
                "display_url" = "instagr.am/p/R-vI40kW3-/";
                "expanded_url" = "http://instagr.am/p/R-vI40kW3-/";
                indices =                     (
                    109,
                    129
                );
                url = "http://t.co/o6armOgJ";
            }
        );
        "user_mentions" =             (
                            {
                id = 3686561;
                "id_str" = 3686561;
                indices =                     (
                    59,
                    74
                );
                name = "Jason L. Baptiste";
                "screen_name" = JasonLBaptiste;
            }
        );
    };
    favorited = 0;
    geo =         {
        coordinates =             (
            "40.75536822",
            "-73.96833024999999"
        );
        type = Point;
    };
    id = 268424619332104192;
    "id_str" = 268424619332104192;
    "in_reply_to_screen_name" = "<null>";
    "in_reply_to_status_id" = "<null>";
    "in_reply_to_status_id_str" = "<null>";
    "in_reply_to_user_id" = "<null>";
    "in_reply_to_user_id_str" = "<null>";
    place =         {
        attributes =             {
        };
        "bounding_box" =             {
            coordinates =                 (
                                    (
                                            (
                        "-74.047285",
                        "40.679548"
                    ),
                                            (
                        "-73.907",
                        "40.679548"
                    ),
                                            (
                        "-73.907",
                        "40.882214"
                    ),
                                            (
                        "-74.047285",
                        "40.882214"
                    )
                )
            );
            type = Polygon;
        };
        country = "United States";
        "country_code" = US;
        "full_name" = "Manhattan, NY";
        id = 086752cb03de1d5d;
        name = Manhattan;
        "place_type" = city;
        url = "https://api.twitter.com/1.1/geo/id/086752cb03de1d5d.json";
    };
    "possibly_sensitive" = 0;
    "retweet_count" = 1;
    retweeted = 0;
    source = "<a href=\"http://instagr.am\" rel=\"nofollow\">Instagram</a>";
    text = "\"30?!? I remember when there were 2 employees!\" catchup w/ @jasonlbaptiste of @onswip @ The Smith Restaurant http://t.co/o6armOgJ";
    truncated = 0;
    user =         {
        "contributors_enabled" = 0;
        "created_at" = "Wed Jul 28 02:43:40 +0000 2010";
        "default_profile" = 0;
        "default_profile_image" = 0;
        description = "VP @Blackstone PE Ops group. Advisor: health NewCos & @TechStars network incubator @BpHealth. Ex-VC @SparkCapital. HBS healthcare cases author. Tweets are mine.";
        entities =             {
            description =                 {
                urls =                     (
                );
            };
            url =                 {
                urls =                     (
                                            {
                        "expanded_url" = "<null>";
                        indices =                             (
                            0,
                            27
                        );
                        url = "http://thesignaltonoise.com";
                    }
                );
            };
        };
        "favourites_count" = 349;
        "follow_request_sent" = "<null>";
        "followers_count" = 1580;
        following = "<null>";
        "friends_count" = 230;
        "geo_enabled" = 1;
        id = 171754967;
        "id_str" = 171754967;
        "is_translator" = 0;
        lang = en;
        "listed_count" = 92;
        location = "NYC & BK";
        name = "Charles Huang";
        notifications = "<null>";
        "profile_background_color" = C0DEED;
        "profile_background_image_url" = "http://a0.twimg.com/profile_background_images/413900237/IMG00062-20090521-1153.jpg";
        "profile_background_image_url_https" = "https://si0.twimg.com/profile_background_images/413900237/IMG00062-20090521-1153.jpg";
        "profile_background_tile" = 1;
        "profile_image_url" = "http://a0.twimg.com/profile_images/1561043828/charles_instagram_normal.jpg";
        "profile_image_url_https" = "https://si0.twimg.com/profile_images/1561043828/charles_instagram_normal.jpg";
        "profile_link_color" = 0084B4;
        "profile_sidebar_border_color" = C0DEED;
        "profile_sidebar_fill_color" = DDEEF6;
        "profile_text_color" = 333333;
        "profile_use_background_image" = 1;
        protected = 0;
        "screen_name" = 1CharlesH;
        "statuses_count" = 2833;
        "time_zone" = Quito;
        url = "http://thesignaltonoise.com";
        "utc_offset" = "-18000";
        verified = 0;
    };
};
source = "<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>";
text = "RT @1CharlesH: \"30?!? I remember when there were 2 employees!\" catchup w/ @jasonlbaptiste of @onswip @ The Smith Restaurant http://t.co/ ...";
truncated = 0;
user =     {
    "contributors_enabled" = 0;
    "created_at" = "Sat Apr 07 05:50:05 +0000 2007";
    "default_profile" = 0;
    "default_profile_image" = 0;
    description = "CEO of Onswipe, Author of The Ultralight Startup";
    entities =         {
        description =             {
            urls =                 (
            );
        };
        url =             {
            urls =                 (
                                    {
                    "expanded_url" = "<null>";
                    indices =                         (
                        0,
                        29
                    );
                    url = "http://www.jasonlbaptiste.com";
                }
            );
        };
    };
    "favourites_count" = 548;
    "follow_request_sent" = "<null>";
    "followers_count" = 8860;
    following = 1;
    "friends_count" = 719;
    "geo_enabled" = 1;
    id = 3686561;
    "id_str" = 3686561;
    "is_translator" = 0;
    lang = en;
    "listed_count" = 656;
    location = "New York City,NY";
    name = "Jason L. Baptiste";
    notifications = "<null>";
    "profile_background_color" = 333333;
    "profile_background_image_url" = "http://a0.twimg.com/profile_background_images/560472/trans.png";
    "profile_background_image_url_https" = "https://si0.twimg.com/profile_background_images/560472/trans.png";
    "profile_background_tile" = 0;
    "profile_image_url" = "http://a0.twimg.com/profile_images/1666517323/me-ignition_normal.jpg";
    "profile_image_url_https" = "https://si0.twimg.com/profile_images/1666517323/me-ignition_normal.jpg";
    "profile_link_color" = 5454DC;
    "profile_sidebar_border_color" = 730000;
    "profile_sidebar_fill_color" = FFFFFF;
    "profile_text_color" = 000000;
    "profile_use_background_image" = 0;
    protected = 0;
    "screen_name" = JasonLBaptiste;
    "statuses_count" = 16625;
    "time_zone" = Hawaii;
    url = "http://www.jasonlbaptiste.com";
    "utc_offset" = "-36000";
    verified = 0;
};
}
2012-11-13 20:01:20.732 MicroDebates[1108:1a03] value is: {
contributors = "<null>";
coordinates = "<null>";
"created_at" = "Tue Nov 13 18:50:15 +0000 2012";
entities =     {
    hashtags =         (
    );
    urls =         (
                    {
            "display_url" = "nytimes.com/2012/11/13/opi\U2026";
            "expanded_url" = "http://www.nytimes.com/2012/11/13/opinion/bruni-the-siren-and-the-spook.html?_r=0";
            indices =                 (
                94,
                114
            );
            url = "http://t.co/aD081T2V";
        }
    );
    "user_mentions" =         (
                    {
            id = 16109501;
            "id_str" = 16109501;
            indices =                 (
                3,
                14
            );
            name = "Azmat Khan";
            "screen_name" = AzmatZahra;
        }
    );
};
favorited = 0;
geo = "<null>";
id = 268425561238560768;
"id_str" = 268425561238560768;
"in_reply_to_screen_name" = "<null>";
"in_reply_to_status_id" = "<null>";
"in_reply_to_status_id_str" = "<null>";
"in_reply_to_user_id" = "<null>";
"in_reply_to_user_id_str" = "<null>";
place = "<null>";
"possibly_sensitive" = 0;
"retweet_count" = 1;
retweeted = 0;
source = "<a href=\"http://www.tweetdeck.com\" rel=\"nofollow\">TweetDeck</a>";
text = "MT @AzmatZahra: A much-needed critique of the sexist clich\U00e9s ubiquitous to Petraeus coverage: http://t.co/aD081T2V";
truncated = 0;
user =     {
    "contributors_enabled" = 0;
    "created_at" = "Sat Feb 03 03:22:09 +0000 2007";
    "default_profile" = 0;
    "default_profile_image" = 0;
    description = "Asst Prof @ University of Michigan School of Information. Social media + HCI.";
    entities =         {
        description =             {
            urls =                 (
            );
        };
        url =             {
            urls =                 (
                                    {
                    "expanded_url" = "<null>";
                    indices =                         (
                        0,
                        33
                    );
                    url = "http://yardi.people.si.umich.edu/";
                }
            );
        };
    };
    "favourites_count" = 320;
    "follow_request_sent" = "<null>";
    "followers_count" = 1582;
    following = 1;
    "friends_count" = 768;
    "geo_enabled" = 0;
    id = 750093;
    "id_str" = 750093;
    "is_translator" = 0;
    lang = en;
    "listed_count" = 137;
    location = "Ann Arbor";
    name = "Sarita Yardi";
    notifications = "<null>";
    "profile_background_color" = 8B542B;
    "profile_background_image_url" = "http://a0.twimg.com/images/themes/theme8/bg.gif";
    "profile_background_image_url_https" = "https://si0.twimg.com/images/themes/theme8/bg.gif";
    "profile_background_tile" = 0;
    "profile_image_url" = "http://a0.twimg.com/profile_images/2152674556/image_normal";
    "profile_image_url_https" = "https://si0.twimg.com/profile_images/2152674556/image_normal";
    "profile_link_color" = 9D582E;
    "profile_sidebar_border_color" = D9B17E;
    "profile_sidebar_fill_color" = EADEAA;
    "profile_text_color" = 333333;
    "profile_use_background_image" = 1;
    protected = 0;
    "screen_name" = syardi;
    "statuses_count" = 12901;
    "time_zone" = "Eastern Time (US & Canada)";
    url = "http://yardi.people.si.umich.edu/";
    "utc_offset" = "-18000";
    verified = 0;
};
}

如果您使用以下搜索查询 twitter,这就是您得到的结果:

NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/search/tweets.json"];
NSMutableDictionary *parameters =
[[NSMutableDictionary alloc] init];
[parameters setObject:@"#energyalt" forKey:@"q"];
[parameters setObject:@"100" forKey:@"count"];
[parameters setObject:@"false" forKey:@"include_entities"];

JSON 响应被解析为一个 NSDictionary,其中包含一个键“状态”和一个由所有推文一起生成的单个值(我想是搜索的值):

(
    {
    contributors = "<null>";
    coordinates = "<null>";
    "created_at" = "Mon Nov 12 21:11:07 +0000 2012";
    favorited = 0;
    geo = "<null>";
    id = 268098624905416705;
    "id_str" = 268098624905416705;
    "in_reply_to_screen_name" = "<null>";
    "in_reply_to_status_id" = "<null>";
    "in_reply_to_status_id_str" = "<null>";
    "in_reply_to_user_id" = "<null>";
    "in_reply_to_user_id_str" = "<null>";
    metadata =         {
        "iso_language_code" = lt;
        "result_type" = recent;
    };
    place = "<null>";
    "retweet_count" = 0;
    retweeted = 0;
    source = "<a href=\"http://www.apple.com\" rel=\"nofollow\">iOS</a>";
    text = "#energyAlt prima prova da iOS $paolotorroni";
    truncated = 0;
    user =         {
        "contributors_enabled" = 0;
        "created_at" = "Sun Aug 09 22:14:15 +0000 2009";
        "default_profile" = 1;
        "default_profile_image" = 0;
        description = "sociologist, network analyst, agent-based modeler";
        entities =             {
            description =                 {
                urls =                     (
                );
            };
            url =                 {
                urls =                     (
                                            {
                        "expanded_url" = "<null>";
                        indices =                             (
                            0,
                            32
                        );
                        url = "http://flavors.me/digitaldust_it";
                    }
                );
            };
        };
        "favourites_count" = 20;
        "follow_request_sent" = "<null>";
        "followers_count" = 26;
        following = "<null>";
        "friends_count" = 72;
        "geo_enabled" = 1;
        id = 64267215;
        "id_str" = 64267215;
        "is_translator" = 0;
        lang = en;
        "listed_count" = 3;
        location = "43.387293,10.445572";
        name = "simone gabbriellini";
        notifications = "<null>";
        "profile_background_color" = C0DEED;
        "profile_background_image_url" = "http://a0.twimg.com/images/themes/theme1/bg.png";
        "profile_background_image_url_https" = "https://si0.twimg.com/images/themes/theme1/bg.png";
        "profile_background_tile" = 0;
        "profile_image_url" = "http://a0.twimg.com/profile_images/2745846822/da14d3f2d7e357c85456909b0bf695b7_normal.jpeg";
        "profile_image_url_https" = "https://si0.twimg.com/profile_images/2745846822/da14d3f2d7e357c85456909b0bf695b7_normal.jpeg";
        "profile_link_color" = 0084B4;
        "profile_sidebar_border_color" = C0DEED;
        "profile_sidebar_fill_color" = DDEEF6;
        "profile_text_color" = 333333;
        "profile_use_background_image" = 1;
        protected = 0;
        "screen_name" = "digitaldust_it";
        "statuses_count" = 396;
        "time_zone" = "<null>";
        url = "http://flavors.me/digitaldust_it";
        "utc_offset" = "<null>";
        verified = 0;
    };
},
    {
    contributors = "<null>";
    coordinates = "<null>";
    "created_at" = "Fri Nov 09 11:19:53 +0000 2012";
    favorited = 0;
    geo = "<null>";
    id = 266862670265868288;
    "id_str" = 266862670265868288;
    "in_reply_to_screen_name" = "<null>";
    "in_reply_to_status_id" = "<null>";
    "in_reply_to_status_id_str" = "<null>";
    "in_reply_to_user_id" = "<null>";
    "in_reply_to_user_id_str" = "<null>";
    metadata =         {
        "iso_language_code" = en;
        "result_type" = recent;
    };
    place = "<null>";
    "retweet_count" = 0;
    retweeted = 0;
    source = "<a href=\"http://www.digitaldust.it/twitter_like\" rel=\"nofollow\">digitaldust</a>";
    text = "$sugarmills productivity is also tied to policy for selling energy, and in Brazil and India is convenient !$windmills #energyalt";
    truncated = 0;
    user =         {
        "contributors_enabled" = 0;
        "created_at" = "Sun Aug 09 22:14:15 +0000 2009";
        "default_profile" = 1;
        "default_profile_image" = 0;
        description = "sociologist, network analyst, agent-based modeler";
        entities =             {
            description =                 {
                urls =                     (
                );
            };
            url =                 {
                urls =                     (
                                            {
                        "expanded_url" = "<null>";
                        indices =                             (
                            0,
                            32
                        );
                        url = "http://flavors.me/digitaldust_it";
                    }
                );
            };
        };
        "favourites_count" = 20;
        "follow_request_sent" = "<null>";
        "followers_count" = 26;
        following = "<null>";
        "friends_count" = 72;
        "geo_enabled" = 1;
        id = 64267215;
        "id_str" = 64267215;
        "is_translator" = 0;
        lang = en;
        "listed_count" = 3;
        location = "43.387293,10.445572";
        name = "simone gabbriellini";
        notifications = "<null>";
        "profile_background_color" = C0DEED;
        "profile_background_image_url" = "http://a0.twimg.com/images/themes/theme1/bg.png";
        "profile_background_image_url_https" = "https://si0.twimg.com/images/themes/theme1/bg.png";
        "profile_background_tile" = 0;
        "profile_image_url" = "http://a0.twimg.com/profile_images/2745846822/da14d3f2d7e357c85456909b0bf695b7_normal.jpeg";
        "profile_image_url_https" = "https://si0.twimg.com/profile_images/2745846822/da14d3f2d7e357c85456909b0bf695b7_normal.jpeg";
        "profile_link_color" = 0084B4;
        "profile_sidebar_border_color" = C0DEED;
        "profile_sidebar_fill_color" = DDEEF6;
        "profile_text_color" = 333333;
        "profile_use_background_image" = 1;
        protected = 0;
        "screen_name" = "digitaldust_it";
        "statuses_count" = 396;
        "time_zone" = "<null>";
        url = "http://flavors.me/digitaldust_it";
        "utc_offset" = "<null>";
        verified = 0;
    };
}
)

最好的,西蒙娜

4

0 回答 0