jsonswiftalamofireswifty-json

responseJSON json parse in Alamofire


I am getting json form server. But json parsing nil in swift. Here is my json

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

I tried bellow code to out_code using Alamofire & swiftjson

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== in print like bellow

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

But I did not get data from out_code key. Please help me what is wrong with my code...


Solution

  • Apparently you are using SwiftyJSON library.

    Casting for each type is been handled from the library. Try using the following code instead of casting to String:

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