2016-10-23 320 views
-1

我想解析JSON,但它不工作。我想從URL中的JSON獲取id,但它顯示爲空值。嘗試解析json時出錯?

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

哪個值完全是'零'? – rmaddy

+0

我想解析URL中的值「id」並打印它 –

+0

再次,哪一行恰恰向您顯示「null」值? – rmaddy

回答

1

關鍵id值是String沒有[String:AnyObject]又名jsonStandard

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

考慮到斯威夫特3 JSON字典的類型是[String:Any]

typealias jsonStandard = [String : Any]