我正在從我的應用程序中解析json的方式。解析JSON數組
我的代碼:
var bytes: NSMutableData?
@IBAction func loadJson(sender: AnyObject) {
let request = NSURLRequest(URL: NSURL(string: "http://api.randomuser.me/")!)
let loader = NSURLConnection(request: request, delegate: self, startImmediately: true)
}
func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
self.bytes = NSMutableData()
}
func connection(connection: NSURLConnection!, didReceiveData conData: NSData!) {
self.bytes?.appendData(conData)
}
func connectionDidFinishLoading(connection: NSURLConnection!) {
let jsonResult: Dictionary = NSJSONSerialization.JSONObjectWithData(self.bytes!, options: NSJSONReadingOptions.MutableContainers, error: nil) as! Dictionary<String, AnyObject>
let results: NSArray = jsonResult["results"]! as! NSArray
println(results)
for result in results {
// Works
println(result)
// Works
println(result["seed"])
// Does not work !!! why?
println(result["user"]["email"])
}
}
爲什麼我不能得到數組電子郵件?
如果你想要的話,你可以看看上面鏈接中的json。
而不是鏈接到JSON,請直接將其包含在問題中。爲了讓您的問題保持在話題上,我刪除了您問題中基於意見的部分。但最終,這個問題仍然處於邊緣。你能詳細說明嗎?//不行!爲什麼?'「不起作用」是什麼意思? – nhgrif