-2
我正在開發一個函數,它在Swift中調用NSURLRequest
後接收到一些JSON。在解析結果後,我遇到了使用下標訪問不同數據值的問題。獲取數據後swift json類型數據解析
以下是檢索的JSON:
{"retCode":100,"retMsg":"Success","retData":{"usn":92,"id":"[email protected]","nickname":"ppigimi","profile_image":"..\/upload\/profile\/20150528172839.jpeg","language":"jp","join_channel_nm":"korfolk","cert_key":"696D6FB453DC141E5295E9D8E37B8DD0F1AFC8E34CE30B74551ED74A447AC564","cert_flag":"Y","join_date":"20150518155650","token":"3ea0a5fec1b55a5a23b5f1dc5c14b040dcd71eea"}}
以下是我的代碼。我不知道如何獲取retData
中的值usn
和token
。
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
println("error=\(error)")
return
}
// You can print out response object
println("response = \(response)")
// Print out response body
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
println("responseString = \(responseString)")
//Let's convert response sent from a server side script to a NSDictionary object:
var err: NSError?
var myJSON = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error:&err) as? NSDictionary
if let parseJSON = myJSON as? [String: AnyObject] {
// Now we can access value of First Name by its key
var retCode = parseJSON["retCode"] as? Int
println("retCode: \(retCode)")
if let retData = parseJSON["retData"] as? [AnyObject] {
for data in retData {
/**let usn = data["usn"]
println("USN = \(usn)")**/
}
}
if retCode == 100 {
//NSUserDefaults.standardUserDefaults().setValue("usn", forKey: String)
}
dispatch_async(dispatch_get_main_queue(), {
var myAlert = UIAlertController(title: "Alert", message: "AAAAAA", preferredStyle: UIAlertControllerStyle.Alert);
let okAction = UIAlertAction(title: "OK", style:UIAlertActionStyle.Default) {
action in
self.dismissViewControllerAnimated(true, completion: nil);
}
myAlert.addAction(okAction);
self.presentViewController(myAlert, animated: true, completion: nil);
});
}
}
task.resume()
如果我的回答你的問題是有幫助的,並履行,那麼請接受它作爲回答您的向其他用戶表明您的問題已得到解決:) http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Kumuluzz