gpt4 book ai didi

ios - 我有 json 响应,但它是可选的,任何人都可以告诉我如何解开可选的 json

转载 作者:行者123 更新时间:2023-11-30 11:17:44 25 4
gpt4 key购买 nike

请告诉我如何解开这个 josn 响应,因为我无法在标签中解析可选的 json

我已经尽力了,但我是 Swift 新手,所以我无法让它解开可选的 json回复::

Optional(<__NSArrayM 0x600000848340>(
{
contributors = "";
coordinates = "";
"created_at" = "Thu Jul 12 11:49:57 +0000 2018";
entities = {
hashtags = (
);
media = (
{
"display_url" = "pic.twitter.com/IJtq6aLM7K";
"expanded_url" = "";
id = 1017375504448479232;
"id_str" = 1017375504448479232;
indices = (
12,
35
);
"media_url" = "";
"media_url_https" = "";
sizes = {
large = {
h = 1820;
resize = fit;
w = 2048;
};
medium = {
h = 1067;
resize = fit;
w = 1200;
};
small = {
h = 604;
resize = fit;
w = 680;
};
thumb = {
h = 150;
resize = crop;
w = 150;
};
};
type = photo;
url = "";
}
);
symbols = (
);
urls = (
);
"user_mentions" = (
);
};
"extended_entities" = {
media = (
{
"display_url" = "pic.twitter.com/IJtq6aLM7K";
"expanded_url" = "";
id = 1017375504448479232;
"id_str" = 1017375504448479232;
indices = (
12,
35
);
"media_url" = "";
"media_url_https" = "";
sizes = {
large = {
h = 1820;
resize = fit;
w = 2048;
};
medium = {
h = 1067;
resize = fit;
w = 1200;
};
small = {
h = 604;
resize = fit;
w = 680;
};
thumb = {
h = 150;
resize = crop;
w = 150;
};
};
type = photo;
url = "";
}
);
};
"favorite_count" = 0;
favorited = 0;
geo = "";
id = 1017375507174719488;
"id_str" = 1017375507174719488;
"in_reply_to_screen_name" = "";
"in_reply_to_status_id" = "";
"in_reply_to_status_id_str" = "";
"in_reply_to_user_id" = "";
"in_reply_to_user_id_str" = "";
"is_quote_status" = 0;
lang = fr;
place = "";
"possibly_sensitive" = 0;
"retweet_count" = 0;
retweeted = 0;
source = "";
text = "Tweet Tweet ";
truncated = 0;
user = {
"contributors_enabled" = 0;
"created_at" = "Tue Mar 27 05:14:33 +0000 2018";
"default_profile" = 1;
"default_profile_image" = 0;
description = "";
entities = {
description = {
urls = (
);
};
};
"favourites_count" = 3;
"follow_request_sent" = 0;
"followers_count" = 1;
following = 0;
"friends_count" = 0;
"geo_enabled" = 1;
"has_extended_profile" = 0;
id = 978500498897563648;
"id_str" = 978500498897563648;
"is_translation_enabled" = 0;
"is_translator" = 0;
lang = en;
"listed_count" = 0;
location = "";
name = Mike;
notifications = 0;
"profile_background_color" = F5F8FA;
"profile_background_image_url" = "";
"profile_background_image_url_https" = "";
"profile_background_tile" = 0;
"profile_image_url" = "";
"profile_image_url_https" = "";
"profile_link_color" = 1DA1F2;
"profile_sidebar_border_color" = C0DEED;
"profile_sidebar_fill_color" = DDEEF6;
"profile_text_color" = 333333;
"profile_use_background_image" = 1;
protected = 0;
"screen_name" = Mike50430315;
"statuses_count" = 13;
"time_zone" = "";
"translator_type" = none;
url = "";
"utc_offset" = "";
verified = 0;
};
}

因此,我在所有类似名称文本转发中都获得了零值,请告诉我如何解决这个问题

 var timeline = (FHSTwitterEngine.shared().getTimelineForUser(FHSTwitterEngine.shared().authenticatedUsername, isID: true, count: 10), terminator: "")

从这一行我得到了完整的回复

然后我创建数组并将其存储到数组中,如下所示

var serviceData = [AnyObject]()
let timelinedata = [timeline] as [AnyObject]
serviceData = (timelinedata)
print(serviceData)

最佳答案

一般来说,任何可选的都可以像这样展开:

let optionalValue: String? = "Hello, this is optional!"

if let noLongerOptional = optionalValue {
print("\(noLongerOptional) no its not! ahah")
}
<小时/>

编辑:更具体的示例...

假设您有一个变量“myJson”来保存您的可选值...并且您做到了:

print("\(myJson)")

您可以通过以下方式解决此问题:

if let validJson = myJson {
print("\(validJson)")
}
<小时/>

编辑 2:...FHSTwitter...(已更新...这已经很长了)

let twitterEngine = FHSTwitterEngine.sharedEngine()
let twitterUser = twitterEngine.authenticatedID
let timeline = twitterEngine.getTimelineForUser(twitterUser, isID: true, count: 10)

根据 FHSTwitterEngine 文档... getTimelineForUser() 将返回 NSError 或推文数组。

所以...

if let error = timeline as? Error {
print("Oops, looks like something went wrong: \(error)")
} else {
if let tweets = timeline as? [Any] {
for tweet in tweets {
print("This is a tweet: \(tweet)")
}
}
}

如果您在使用 FHSTwitterEngine 时遇到更多问题,我建议您使用:https://github.com/natesymer/FHSTwitterEngine/tree/master/FHSTwitterEngineDemoSwift/Demo-Swift

或者向 FHSTwitterEngine 开发人员提交支持票:https://github.com/natesymer/FHSTwitterEngine/issues

祝你好运!

关于ios - 我有 json 响应,但它是可选的,任何人都可以告诉我如何解开可选的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51592423/

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