11

I'm using Alamofire as HTTP library, since the update to Swift 3, how do you parse JSON based on the example below?

Alamofire.request("https://httpbin.org/get").responseJSON { response in
    debugPrint(response)

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

respone.result.value is of Any object, and is very new and confusing.

Lawrence Gimenez
  • 2,662
  • 4
  • 34
  • 52
  • 1
    Isn't it a duplicate of http://stackoverflow.com/questions/35447161/need-help-parsing-json-with-swift-using-alamofire?rq=1 ? – Martin Delille Sep 13 '16 at 11:10
  • 1
    @MartinDelille it is the same but I think it is still using the Swift 2, there are a lot of breaking changes and SwiftyJSOn has not yet been upgrade to latest Swift 3 – Lawrence Gimenez Sep 13 '16 at 13:55
  • @LawGimenez See update for using SwiftyJSON with Swift 3. – mixel Sep 13 '16 at 14:05
  • Did you really have to put "Swift 3" twice in the title? – Léo Natan Oct 31 '16 at 19:10
  • 3
    @LeoNatan I'm sorry I was in a hurry with deadlines and all, no idea this was such a big deal in StackOverflow. It won't happen again in the future I promise. Just came back here and edited the title so you can sleep peacefully and all. – Lawrence Gimenez Nov 01 '16 at 13:14

3 Answers3

19

As you can see in Alamofire tests you should cast response.result.value to [String:Any]:

if let json = response.result.value as? [String: Any] {
  // ...
}
mixel
  • 25,177
  • 13
  • 126
  • 165
3

Updated for swift 3 :

if your response is like below,

[
    {
        "uId": 1156,
        "firstName": "Kunal",
        "lastName": "jadhav",
        "email": "kunal@gmail.com",
        "mobile": "7612345631",
        "subuserid": 4,
        "balance": 0
    }
]

**if you want to parsing the above JSON response used below simple lines of code: **

    Alamofire.request(yourURLString, method: .get, encoding: JSONEncoding.default)
        .responseJSON { response in
            debugPrint(response)

            if let data = response.result.value{

                if  (data as? [[String : AnyObject]]) != nil{

                    if let dictionaryArray = data as? Array<Dictionary<String, AnyObject?>> {
                        if dictionaryArray.count > 0 {

                            for i in 0..<dictionaryArray.count{

                                let Object = dictionaryArray[i]
                                if let email = Object["email"] as? String{
                                    print("Email: \(email)")
                                }
                                if let uId = Object["uId"] as? Int{
                                    print("User Id: \(uId)")
                                }
                                // like that you can do for remaining...
                            }
                        }
                    }
                }
            }
            else {
                let error = (response.result.value  as? [[String : AnyObject]])
                print(error as Any)
            }
    }
Kiran Jadhav
  • 3,209
  • 26
  • 29
2

If you don't want to use SwiftyJson do this with Alamofire 4.0:

Alamofire.request("https://httpbin.org/get").responseString { response in
    debugPrint(response)

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

The key point being use responseString instead of responseJSON.

Source: https://github.com/Alamofire/Alamofire#response-string-handler

Micro
  • 10,303
  • 14
  • 82
  • 120
  • You do not need SwiftyJSON to use `responseJSON`. – mixel Oct 31 '16 at 19:04
  • @mixel yeah I know. The `responseJSON` was giving me issues in swift 3 + alamofire 4.0 though when I updated my app. Changing it to `responseString` helped me solve the issue without having to change anything else. – Micro Oct 31 '16 at 19:11