gpt4 book ai didi

json - 使用 TwitterKit 在 Swift 中解析 Twitter API 搜索响应

转载 作者:行者123 更新时间:2023-11-30 12:03:12 24 4
gpt4 key购买 nike

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

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

此外,任何关于为什么我正在查看的“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 - 使用 TwitterKit 在 Swift 中解析 Twitter API 搜索响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46964712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com