首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用TwitterKit分析Swift中TwitterKit搜索响应

用TwitterKit分析Swift中TwitterKit搜索响应
EN

Stack Overflow用户
提问于 2017-10-26 22:20:06
回答 1查看 937关注 0票数 0

我试图只获取twitter调用返回的文本、用户名和配置文件图像,并使用它们的引用:Twitter :搜索Tweets。我得到了一个真实的响应,并且可以按状态解析JSON,但是每个状态的内容似乎都是一种不是JSON的格式,因为每个“键”都不是引号,并且使用相等号来关联它的值。

有谁知道我怎样才能获得我最终得到的‘Stat’对象,并从中提取任何一个值?即使只是将其转换为字符串,我也可以使用字符串解析来提取它。这只是一个学习项目,它不需要花哨。

另外,对于我所看到的' JSON‘为什么实际上不遵循正常的JSON格式,任何解释都是有帮助的。

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

代码语言:javascript
复制
    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)")
        }
    }

这是输出,当我打印出一种状态时,我似乎只能将其转换为任何状态。当我试图把它转换成字符串时,它会变成零。

代码语言:javascript
复制
{
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;
};

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-09 00:09:42

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46964712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档