Hoang Aloevera Hoang Aloevera - 1 month ago 11
Swift Question

Error when try to parse json?

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

id
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() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
callAlamo(url: SearchURL)
}

func callAlamo(url : String) {
Alamofire.request(url).responseJSON(completionHandler:
{
responds in
self.parseData(JsonData: responds.data!)
})
}

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

Answer

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]