简体   繁体   中英

i have json response but its optional can any one tell me how to unwrap optional json

please tell me how to unwrap this josn response because of optional json i am not able to parse in label

i have tried my best but i am new to swift so i can't get it to unwrap the optional json response::

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;
    };
}

because of this i am getting nil value in all like name text retweet please tell me how to solve this

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

from this line i am getting whole response

and then i have on array i create and store in to array like below

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

In general, anything optional can be unwrapped like so:

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

if let noLongerOptional = optionalValue {
    print("\(noLongerOptional) no its not! ahah")
}

Edit: more specific example...

Assuming you have a variable 'myJson' which hold your optional value... and you did:

print("\(myJson)")

You can solve this by:

if let validJson = myJson {
  print("\(validJson)")
}

Edit 2: ... FHSTwitter... (updated.... this is getting long)

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

According to FHSTwitterEngine docs... getTimelineForUser() will return either a NSError or an array of Tweets.

so...

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)")
    }
  }
}

If you have any more trouble using FHSTwitterEngine, I suggest you play with: https://github.com/natesymer/FHSTwitterEngine/tree/master/FHSTwitterEngineDemoSwift/Demo-Swift

Or open a support ticket with the FHSTwitterEngine developer(s) at: https://github.com/natesymer/FHSTwitterEngine/issues

Good Luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM