Search code examples
swiftinstagramalamofireinstagram-apiswifty-json

Alamofire Swift Json


I receive Instagram JSON by this method

Alamofire.request("https://www.instagram.com/", 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 dUrl = Object["display_url"] as? String{
                                print("Find: \(dUrl)")
                            }
                            if let id = Object["id"] as? String{
                                print("User Id: \(id)")
                            }
                        }
                    }
                }
            }
        }
        else {
            let error = (response.result.value  as? [[String : AnyObject]])
            print(error as Any)
        }
}

But please help me. How I can receive: - id and - third link of display_resources[2]


Solution

  • SwiftyJSON documentation is well detailed: https://github.com/SwiftyJSON/SwiftyJSON

    let json = JSON(response.result.value ?? [])
    let id = json["shortcode_media"]["id"].stringValue
    let url = json["shortcode_media"]["display_url"]["display_resources"][2]["src"].stringValue