Hoang Aloevera Hoang Aloevera - 9 months ago 60
Swift Question

Error when try to parse json?

I'm trying to parse JSON, but it's not working. I want to get

from the JSON at the URL, but it shows me null value.

var names = [String]()
var SearchURL = "http://ios.khothe.vn/web/gamecards/authenticate/user/dungphiau/pass/829d81d46bad96825dc52a6e1675aab0"
typealias jsonStandard = [String : AnyObject]

override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.
callAlamo(url: SearchURL)

func callAlamo(url : String) {
responds in
self.parseData(JsonData: responds.data!)

func parseData(JsonData : Data) {
var readableJson = try JSONSerialization.jsonObject(with: JsonData, options: .mutableContainers) as! jsonStandard
let tracks = readableJson["id"] as? jsonStandard
} catch{

Answer Source

The value for key id is String not [String:AnyObject] aka jsonStandard

let tracks = readableJson["id"] as? String

Consider that in Swift 3 the type of a JSON dictionary is [String:Any]

typealias jsonStandard = [String : Any]