尝试解析json时出错?

时间:2016-10-23 17:07:40

标签: json swift swift3 alamofire

我试图解析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)
     }
 }

1 个答案:

答案 0 :(得分:1)

密钥id的值为String而不是[String:AnyObject]又名jsonStandard

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

考虑到在Swift 3中,JSON字典的类型是[String:Any]

typealias jsonStandard = [String : Any]