Swift 从cloudant获取JSON数据

Swift 从cloudant获取JSON数据,swift,cloudant,Swift,Cloudant,我在Cloudant中创建了一个数据库,我正试图在我的Swift项目中解析该数据。我已经导入了Cloudant库,但无法解析数据 这是我一直使用到现在的代码 var url = "https://username:password@cloudant.com/dashboard.html#/database/db_2/login_details" var request = NSMutableURLRequest() request.URL = NSURL(string: url) request

我在Cloudant中创建了一个数据库,我正试图在我的Swift项目中解析该数据。我已经导入了Cloudant库,但无法解析数据

这是我一直使用到现在的代码

var url = "https://username:password@cloudant.com/dashboard.html#/database/db_2/login_details"
var request = NSMutableURLRequest()
request.URL = NSURL(string: url)
request.HTTPMethod = "GET"

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
    var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
    let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary
    if jsonResult != nil {
        if let item = json[0] {
            if let student_name = item["Student_name"] {
                if let pasword = student_name["password"] {
                    println("Login Successfull")
                }
            }
        }
    }
}

有人能告诉我哪里出错了吗?

您应该使用stringbyaddingpercentescapesusingincodingnsutf8stringencoding来转换链接中的任何特殊字符。此外,NSURL可能返回nil,因此您需要使用if let按如下方式处理它:

if let link = "https://username:password@cloudant.com/dashboard.html#/database/db_2/login_details".stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
    if let checkedUrl = NSURL(string: link) {
        println("valid Url")
        // your code
    } else {
        println("invalid Url")
    }
}

尝试展开您的url NSURLstring:url!怎么了?错误?不正确的行为?当你试图运行代码时,飞天怪兽吃掉了你唯一的百吉饼?您需要提供更多关于我无法解析数据的细节。意思是在你的情况下,这个代码什么都没做。。这就是我不解析数据的意思。