1

I have a project in swift where I post a URL and get the result in json. The json reply from the url contains many greek letters and for example instead of "Γ" I get "\U0393". How I can translate the escape characters in swift? My code is the following:

let url = NSURL(string: "https://www.something.that.creates.a.json.array")!

    let task = URLSession.shared.dataTask(with: url as URL) { (data, response, error) -> Void in
        if let urlContent = data {
            do {
                let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers)
                print(jsonResult)
            } catch {
                print("Json Serialization error")
            }
        }
    }
    task.resume()
ToroLoco
  • 461
  • 1
  • 5
  • 20
  • Are you really sure you get things like `\U0393` in actual data instead of that simply appearing when viewing the data in the debugger? What do you see if you show the actual string in a label? – rmaddy Oct 27 '17 at 22:09
  • hmm, haven't test it in label. Only on the debugger. – ToroLoco Oct 27 '17 at 22:11
  • This is a huge json result and I cannot select only the jsonResult["name"] items. It returns an error Type 'Any' has no subscript members – ToroLoco Oct 27 '17 at 22:32
  • Check out this answer: https://stackoverflow.com/questions/39423367/correctly-parsing-json-in-swift-3 – inspector_60 Oct 27 '17 at 22:50

1 Answers1

0

The escape characters where indeed appearing correct as rmaddy wrote. But I had to cast the result as [[String : Any]] in order to access the Dictionary inside the array correctly.

let url = NSURL(string: "https://www.something.that.creates.a.json.array")!

let task = URLSession.shared.dataTask(with: url as URL) { (data, response, error) -> Void in
    if let urlContent = data {
        do {
            let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as! [[String : Any]]
            print(jsonResult[0]["description"])
        } catch {
            print("Json Serialization error")
        }
    }
}
task.resume()
ToroLoco
  • 461
  • 1
  • 5
  • 20