首页 > 解决方案 > Alamofire中的responseJSON json解析

问题描述

我正在获取 json 表单服务器。但是 json 迅速解析 nil。这是我的json

{
    "out_code":"0",
    "out_message":"0",
}

我尝试使用 Alamofire 和 swiftjson 将下面的代码输出到 out_code

func getData(){
    
    Alamofire.request("url", method: .post,parameters: [
        "request_code":"11"
        
    ]).responseJSON{(responseData) -> Void in
        if((responseData.result.value != nil)){
            
            let swiftyJsonVar = JSON(responseData.result.value!)
            print("swiftyJsonVar==",swiftyJsonVar)
            
            if let v_id = swiftyJsonVar["out_code"] as? String {
                print("out_code----",v_id)
            }
            
        }
    }
}

swiftyJsonVar==打印如下

{
    "out_code" : "0",
    "out_message" : null
}

但我没有从out_code键中获取数据。请帮我看看我的代码有什么问题...

标签: jsonswiftalamofireswifty-json

解决方案


显然您正在使用 SwiftyJSON 库。

每种类型的转换都是从库中处理的。尝试使用以下代码而不是强制转换为String

if let v_id = swiftyJsonVar["out_code"].string {
    print("out_code----",v_id)
}

推荐阅读