Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting data from AF.Request response

I need the data from the json response from my Post request call using Alamofire but I cannot access that data for some reason

I tried following along with Alamofire github documentation along with this post get data from AF responseJSON. but neither have helped me.

AF.request("https://mbd.cookcountysupernetwork.com/ap/swift_math_get.asp", method: .post,  parameters: parameters, encoding: JSONEncoding.default)
            .responseJSON { response in
                print(response)
                print("floop")

        }

This is what I see when the code runs

success({
    Operand =     (
                {
            A = 12;
        },
                {
            B = 25;
        }
    );
    Operation = Multiply;
    Result = 300;
})

so I know the json is there, i just need to access the "Result = 300" so I can set a text box to say "300". but I tried many different methods and I cannot access the information I need from response. Also i do not have a response.result.value which is what almost every post I see about this says to use.

like image 727
Andrew Chao Avatar asked Aug 15 '19 04:08

Andrew Chao


People also ask

How do I parse JSON response from Alamofire API in Swift?

To parse the response in Alamofire API request, we will use JSONDecoder, which is an object that decodes instances of a data type from JSON objects. The decode method of JSONDecoder is used to decode the JSON response. It returns the value of the type we specify, decoded from a JSON object.

What is the use of Alamofire?

Alamofire is a networking library written in Swift. You use it to make HTTP(S) requests on iOS, macOS and other Apple platforms. For example, to post data to a web-based REST API or to download an image from a webserver. Alamofire has a convenient API built on top of URLSession (“URL Loading System”).


1 Answers

You can access the Result value as,

AF.request("https://mbd.cookcountysupernetwork.com/ap/swift_math_get.asp", method: .post,  parameters: parameters, encoding: JSONEncoding.default)
        .responseJSON { response in
            switch response.result {
            case .success(let value):
                if let json = value as? [String: Any] {
                    print(json["Result"] as? Int)
                }
            case .failure(let error):
                print(error)
            }
    }
like image 160
Kamran Avatar answered Sep 23 '22 20:09

Kamran