user2423476
user2423476

Reputation: 2275

Parse Alamofire json response

I am trying to parse the response from Alamofire but I can't figure out how to do it.

This is the JSON Response I get (I want to parse out "result") how is this done?

JSON: {
    result = 887957;
    status = 0;
}

Swift 3

if let JSON = response.result.value {
print("JSON: \(JSON)")
}

Upvotes: 0

Views: 1308

Answers (3)

Ravindra Shekhawat
Ravindra Shekhawat

Reputation: 4353

As per latest Almofire Lib and Swift 3.0 with proper validation:

case .success(_):
 if ((response.result.value) != nil) {
  var responseData = JSON(response.result.value!)

  //Userdefaults helps to store session data locally just like sharedpreference in android
  if (response.response ? .statusCode == 200) {
   let result: Int = responseData["result"].int!
   let status: Int = responseData["status"].int!

  } 
 }


case .failure(_):
 print(response.result)
 }

Upvotes: -1

Sreejith S
Sreejith S

Reputation: 376

if let JSON = response.result.value as? [String : Any] {
    let result = JSON["result"] as? Int
    let status = JSON["status"] as? Int
    print("Result \(result) Status \(status)")
}

Upvotes: 0

Nirav D
Nirav D

Reputation: 72410

You just need to specify the type of response is Dictionary and then use subscript with dictionary to get value of result.

if let dictionary = response.result.value as? [String: Int] {

    let result = dictionary["result"] ?? 0
    print(result)
}

Upvotes: 2

Related Questions