我试图解析JSON,但它无法正常工作。我想从URL中的JSON获取id
,但它显示空值。
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)
}
}
答案 0 :(得分:1)
密钥id
的值为String
而不是[String:AnyObject]
又名jsonStandard
let tracks = readableJson["id"] as? String
考虑到在Swift 3中,JSON字典的类型是[String:Any]
typealias jsonStandard = [String : Any]