YellowPillow
YellowPillow

Reputation: 4270

NSJSONSerialization.JSONObjectWithData changing contents of returned results?

func retrieveData(englishWord : String, completionHandler : (success: Bool, data : [String : AnyObject]?, errorString : String?) -> Void) {
        let baseURL = "https://api.pearson.com/v2/dictionaries/ldoce5/entries?headword=vodka"
        let urlString = baseURL
        print(urlString)
        let url = NSURL(string: urlString)
        let request = NSMutableURLRequest(URL: url!)
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithRequest(request) { data, response, error in
            if error != nil {
                completionHandler(success: false, data: nil, errorString: "There was a networking error")
                return
            }
            if data == nil {
                completionHandler(success: false, data: nil, errorString: "There was an error in the request for data")
                return
            }

            let parsedResult : AnyObject?
            do {
                parsedResult = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as! [String : AnyObject]
                print(parsedResult!)

            } catch {
                completionHandler(success: false, data: nil, errorString: "There was an error in the conversion for data")
                return
            }

        }
        task.resume()
    }

So right now print(parsedResult!) gives:

{ count = 1; limit = 10; offset = 0; results = ( { datasets = ( ldoce5, dictionary ); headword = vodka; id = cqAG3cXCxZ; "part_of_speech" = noun; pronunciations = ( { audio = ( { lang = "British English"; type = pronunciation; url = "/v2/dictionaries/assets/ldoce/gb_pron/vodka0205.mp3"; } ); ipa = "\U02c8v\U0252dk\U0259"; }, { audio = ( { lang = "American English"; type = pronunciation; url = "/v2/dictionaries/assets/ldoce/us_pron/vodka.mp3"; } ); ipa = "\U02c8v\U0251\U02d0dk\U0259"; lang = "American English"; } ); senses = ( { definition = ( "a strong clear alcoholic drink originally from Russia, or a glass of this" ); } ); url = "/v2/dictionaries/entries/cqAG3cXCxZ"; } ); status = 200; total = 1; url = "/v2/dictionaries/ldoce5/entries?headword=vodka"; }

But the query URL : Here

I posted it here for convenience:

{"status":200,"offset":0,"limit":10,"count":1,"total":1,"url":"/v2/dictionaries/ldoce5/entries?headword=vodka","results":[{"datasets":["ldoce5","dictionary"],"headword":"vodka","id":"cqAG3cXCxZ","part_of_speech":"noun","pronunciations":[{"audio":[{"lang":"British English","type":"pronunciation","url":"/v2/dictionaries/assets/ldoce/gb_pron/vodka0205.mp3"}],"ipa":"ˈvɒdkə"},{"audio":[{"lang":"American English","type":"pronunciation","url":"/v2/dictionaries/assets/ldoce/us_pron/vodka.mp3"}],"ipa":"ˈvɑːdkə","lang":"American English"}],"senses":[{"definition":["a strong clear alcoholic drink originally from Russia, or a glass of this"]}],"url":"/v2/dictionaries/entries/cqAG3cXCxZ"}]}

Is there a reason why NSJSONSerialization is changing my data as the data printed out is clearly different to what is returned by the Pearson API?

Upvotes: 0

Views: 211

Answers (1)

KirkSpaziani
KirkSpaziani

Reputation: 1972

I don't believe it's actually changing the values, I believe you're just looking at a different representation. (If Swift behaves like Objective-C), the print(x) will call the "description" method on x, which can output the object in any format it likes to.

What you might want to do is reencode the data using: dataWithJSONObject:options:error (the essentially opposite call to what you made), and take a look at what the representation is.

https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/#//apple_ref/occ/clm/NSJSONSerialization/dataWithJSONObject:options:error:

Upvotes: 1

Related Questions