2

Can I decode a JSON file in Swift? I searched a lot in Google, but couldn't find anything.

I have a next code:

let urlPath = "http://example.com/json"
let url = NSURL(string: urlPath)

let session = NSURLSession.sharedSession()

let task = session.dataTaskWithURL(url!, completionHandler: {
        data, response, error -> Void in

if (error != nil) {
    println(error)
} else {
    let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary

    println(jsonResult)
}
})

task.resume()

And as result I gave the next:

\U0440\U0435\U0434\U0441\U0442\U0430\U0432\U043b

I know that this JSON is encoded. How can I decode it and use?

Orkhan Alizade
  • 7,379
  • 14
  • 40
  • 79
  • I'm not quite understanding what your problem/question it. More details please. – BastiBen Apr 06 '15 at 11:12
  • 2
    *There is no problem!* That is just the way that the description method of NSDictionary uses to print non-ASCII characters, compare for example http://stackoverflow.com/questions/17337782/nsdata-to-nsstring-with-json-response. Just extract your needed values from the dictionary and print those. – Martin R Apr 06 '15 at 11:12
  • That looks fine. It is `редставл`. Is that what you were expecting? – Rob Apr 06 '15 at 11:19
  • 1
    @OrkhanAlizade: You don't have to convert anything! Did you have a look at the Q&A that I linked to? Just try `println(jsonResult.objectForKey("yourKey"))` or similar... – Martin R Apr 06 '15 at 11:40
  • @MartinR I read it. But I think that it's my fall! My parser changes the JSON. It's not the same as in the source, but I think I can improve it myself! Thanks! – Orkhan Alizade Apr 06 '15 at 11:55
  • By the way, working with JSON in Swift is horrible with all optional unwrapping and and and, so I'd suggest using SwiftyJSON - you can download the code on github, simply search for it on Google – borchero Apr 06 '15 at 19:09

0 Answers0