简体   繁体   English

如何使用 Alamofire 在 Xcode 8 Swift 3.0 中获取特定的 JSON?

[英]How to get specific JSON in Xcode 8 Swift 3.0 using Alamofire?

I have these JSON data我有这些 JSON 数据

{
    "ID":"ID01",
    "pass":"1234",
    "mail":"email@g.com"
}

I'm trying to display ID only using Alamofire, all JSON object can be print it out.我试图仅使用 Alamofire 显示 ID,所有 JSON 对象都可以打印出来。

But when i try to print specifically, an error appears "type 'Any' has no subscript members" on line但是当我尝试专门打印时,在线出现错误“类型'Any'没有下标成员”

print(JSON["ID"] as! String)

The code as below代码如下

import UIKit
import Alamofire
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        Alamofire.request("http://localhost/get.php").responseJSON
            { response in

                if let JSON = response.result.value {
                    print("JSON: \(JSON)")
                    print(JSON["ID"] as! String)
                }
            }   
    }
}

You should try to cast value of the response.您应该尝试转换响应的值。

How about:怎么样:

if let JSON = response.result.value as? [String : Any]{
    print("JSON: \(JSON)")
    if let id = JSON["ID"] as? String {
        print(JSON["ID"])
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM