我有json响应,但它的可选项可以告诉我如何解开可选的json。

时间:2018-07-30 10:56:22

标签: ios json swift

请告诉我,由于可选的json,我无法解析此josn响应,我无法在标签中进行解析

我已经尽力了,但是我是新手,所以我无法解开可选的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)

1 个答案:

答案 0 :(得分:0)

通常,任何可选内容都可以像这样展开:

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或Tweets数组。

所以...

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

祝你好运!