【问题标题】:Parsing Twitter API Search Response in Swift with TwitterKit使用 TwitterKit 在 Swift 中解析 Twitter API 搜索响应
【发布时间】:2018-04-08 10:47:19
【问题描述】:

我正在尝试仅获取文本、用户名以及稍后由 twitter API 调用返回的推文的个人资料图像,使用它们的参考:Twitter API: Search Tweets。我得到了一个真实的响应,并且可以按状态解析 JSON,但是每个状态的内容似乎都不是 JSON 格式,因为每个“键”都不是在引号中,而是使用等号来关联有它的价值。

有谁知道我如何获取最终得到的“状态”对象并提取任何值?即使只是将其转换为字符串,我也可以使用字符串解析将其拉出。这只是一个学习项目,不需要花哨。

此外,任何关于为什么我正在查看的“JSON”实际上并不遵循正常 JSON 格式的解释都会有所帮助。

我正在使用 TwitterKit 发出请求。我对 swift 和 JSON 解析相当陌生。这是我的代码:

    let client = TWTRAPIClient()
    let statusesShowEndpoint = "https://api.twitter.com/1.1/search/tweets.json"
    let params = ["q": "@formlabs", "result_type": "recent", "include_entities": "false", "count": "3"]
    var clientError : NSError?

    let request = client.urlRequest(withMethod: "GET", url: statusesShowEndpoint, parameters: params, error: &clientError)

    client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
        if connectionError != nil {
            print("Error: \(String(describing: connectionError))")
        }
        do {
            let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: Any]
            //print(json)
            let statuses = json!["statuses"] as? [Any]
            let status = statuses![0]
            print(status)
        } catch let jsonError as NSError {
            print("json error: \(jsonError.localizedDescription)")
        }
    }

这是我打印出一个状态时的输出,我似乎只能将其转换为 Any。当我尝试转换为字符串时,它返回 nil。

{
contributors = "<null>";
coordinates = "<null>";
"created_at" = "Thu Oct 26 21:22:50 +0000 2017";
"favorite_count" = 1;
favorited = 0;
geo = "<null>";
id = 923661218778247169;
"id_str" = 923661218778247169;
"in_reply_to_screen_name" = mesgreene;
"in_reply_to_status_id" = 923623395849367552;
"in_reply_to_status_id_str" = 923623395849367552;
"in_reply_to_user_id" = 71702948;
"in_reply_to_user_id_str" = 71702948;
"is_quote_status" = 0;
lang = en;
metadata =     {
    "iso_language_code" = en;
    "result_type" = recent;
};
place =     {
    attributes =         {
    };
    "bounding_box" =         {
        coordinates =             (
                            (
                                    (
                    "-117.282538",
                    "32.53962"
                ),
                                    (
                    "-116.9274403",
                    "32.53962"
                ),
                                    (
                    "-116.9274403",
                    "33.0804044"
                ),
                                    (
                    "-117.282538",
                    "33.0804044"
                )
            )
        );
        type = Polygon;
    };
    "contained_within" =         (
    );
    country = "United States";
    "country_code" = US;
    "full_name" = "San Diego, CA";
    id = a592bd6ceb1319f7;
    name = "San Diego";
    "place_type" = city;
    url = "https://api.twitter.com/1.1/geo/id/a592bd6ceb1319f7.json";
};
"retweet_count" = 0;
retweeted = 0;
source = "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>";
text = "@mesgreene @HeroForgeMinis @formlabs Formlabs said I would have to apply a primer to it, then I could paint it with\U2026 ";
truncated = 1;
user =     {
    "contributors_enabled" = 0;
    "created_at" = "Wed Jan 16 03:07:48 +0000 2013";
    "default_profile" = 0;
    "default_profile_image" = 0;
    description = "Bridge Designer. Alternative Delivery Expert. ProjectWise Dude. Guru in all things BIM/CAD. And I moonlight as patient support for people with Crohn's / IBD";
    entities =         {
        description =             {
            urls =                 (
            );
        };
        url =             {
            urls =                 (
                                    {
                    "display_url" = "/Zv8TKR";
                    "expanded_url" = "https:///Zv8TKR";
                    indices =                         (
                        0,
                        23
                    );
                    url = "https:///mOUvQ9iP7w";
                }
            );
        };
    };
    "favourites_count" = 3530;
    "follow_request_sent" = "<null>";
    "followers_count" = 238;
    following = "<null>";
    "friends_count" = 232;
    "geo_enabled" = 1;
    "has_extended_profile" = 1;
    id = 1093947408;
    "id_str" = 1093947408;
    "is_translation_enabled" = 0;
    "is_translator" = 0;
    lang = en;
    "listed_count" = 29;
    location = "San Diego, CA";
    name = "SoCal BIM Guru";
    notifications = "<null>";
    "profile_background_color" = 000000;
    "profile_background_image_url" = "http://abs.twimg.com/images/themes/theme1/bg.png";
    "profile_background_image_url_https" = "https://abs.twimg.com/images/themes/theme1/bg.png";
    "profile_background_tile" = 0;
    "profile_banner_url" = "https://pbs.twimg.com/profile_banners/1093947408/1499732034";
    "profile_image_url" = "http://pbs.twimg.com/profile_images/885923732178337792/qnWdWM_J_normal.jpg";
    "profile_image_url_https" = "https://pbs.twimg.com/profile_images/885923732178337792/qnWdWM_J_normal.jpg";
    "profile_link_color" = 1B95E0;
    "profile_sidebar_border_color" = 000000;
    "profile_sidebar_fill_color" = 000000;
    "profile_text_color" = 000000;
    "profile_use_background_image" = 0;
    protected = 0;
    "screen_name" = ReubenJStone;
    "statuses_count" = 1304;
    "time_zone" = "Pacific Time (US & Canada)";
    "translator_type" = none;
    url = "https:///mOUvQ9iP7w";
    "utc_offset" = "-25200";
    verified = 0;
};

}

【问题讨论】:

    标签: json swift api twitter


    【解决方案1】:

    【讨论】:

    • 您是如何使用框架的。例如,您是否有一个获取实体的小代码 sn-p,这将有很大帮助。谢谢!
    • 对不起,这个项目后来演变成不同的东西,然后我放弃了,所以我没有任何样本。
    猜你喜欢
    • 2019-03-13
    • 1970-01-01
    • 2015-07-25
    • 2019-05-18
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多