Rohit
Rohit

Reputation: 965

parsing twitter search json data using NSJSONSerialization

I am parsing twiter search api json data with NSJSONSerialization.Requirement is to search tweets by hashtag.In Twitter api console tool I am correctly getting data about 15 tweets.

written code is

if let results: NSDictionary = NSJSONSerialization .JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments  , error: errorPointer) as? NSDictionary {
             }

I am getting results value as

{
    "search_metadata" =     {
        "completed_in" = "0.05";
        count = 15;
        "max_id" = 680240431771156480;
        "max_id_str" = 680240431771156480;
        "next_results" = "?max_id=680240407322689535&q=%23ChristmasEve&include_entities=1";
        query = "%23ChristmasEve";
        "refresh_url" = "?since_id=680240431771156480&q=%23ChristmasEve&include_entities=1";
        "since_id" = 0;
        "since_id_str" = 0;
    };
    statuses =     (
                {
            contributors = "<null>";
            coordinates = "<null>";
            "created_at" = "Fri Dec 25 04:15:31 +0000 2015";
            entities =             {
                hashtags =                 (
                                        {
                        indices =                         (
                            0,
                            13
                        );
                        text = ChristmasEve;
                    },
                                        {

which is incomplete. I even tried using SwiftyJSon library but I am getting similar results.

Is there any way to get statuses/Tweet info value without using any external library?

Upvotes: 2

Views: 332

Answers (1)

Julian J. Tejera
Julian J. Tejera

Reputation: 1021

Given the fact that you mentioned you are getting multiple tweets (15), the JSON data you're getting back from the API possibly is an array, not a dictionary. It's a good practice to handle both cases when you make network calls:

    do {
        let object = try NSJSONSerialization.JSONObjectWithData(data, options: [])
        if let dictionary = object as? [NSObject: AnyObject] {
            // Handle dictionary
        } else if let array = object as? [[NSObject: AnyObject]] {
            // Handle array
        }

    } catch {

    }

Upvotes: 1

Related Questions