How to get json response using alamofire in iOS Swift?

alamofire json parsing swift 5
alamofire json parsing swift 4
response.result.value alamofire 5
how to use alamofire in swift 4
alamofire get request swift 4
alamofire get request with parameters swift 4
alamofire post request swift 5
alamofire get response data

I have been trying to get json response from url using alamofire. Created model, apirouter and api client class.

its shows error

 failure(Alamofire.AFError.responseSerializationFailed(reason: 
  Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: 
   Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "variables", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"variables\", intValue: nil) (\"variables\").", underlyingError: nil)))))

Here is my postman json response:

   [
{
    "id": "00602c70-fc8a-11e9-ad1d-2abe2670111d",
    "resourceType": "Task",
    "name": "My Tasks",
    "owner": null,
    "query": {
        "assigneeExpression": "${currentUser()}",
        "taskVariables": [],
        "processVariables": [],
        "caseInstanceVariables": [],
        "orQueries": []
    },
    "properties": {
        "variables": [
            {
                "name": "loanAmount",
                "label": "Loan Amount"
            },
            {
                "name": "firstName",
                "label": "First Name"
            }
        ],
        "color": "#555555",
        "showUndefinedVariable": false,
        "description": "Tasks assigned to me",
        "refresh": false,
        "priority": -10
      }
    }
   ]

My trying get values for id, name and properties -> variables -> name and label from json response.

Here is model class:

  import Foundation

public struct Filter: Codable {

let id: String
let name: String
let properties: [variables]


}

public struct variables: Codable {

   let name: String
   let label: String

}

Here is code for alamofire :

    private static func performRequest<T:Decodable>(route:APIRouter, decoder: JSONDecoder = JSONDecoder(), completion:@escaping (AFResult<T>)->Void) -> DataRequest {

    return AF.request(route)
                    .responseDecodable (decoder: decoder){ (response: AFDataResponse<T>) in
                        completion(response.result)
                        print("framework response::",response.result)


    }


}


  public static func getFilter(completion:@escaping (AFResult<[Filter]>)->Void) {
       let jsonDecoder = JSONDecoder()
    performRequest(route: APIRouter.getFilter, decoder: jsonDecoder, completion: completion)
   }

Any help much appreciates pls...

your model class should be like the below.

import Foundation

public struct Filter: Codable {

let id: String
let name: String
let properties: Properties


}

public struct Properties: Codable {
   let variables: [variables]
   let color: String
   let showUndefinedVariable: Bool
   let description: String
   let refresh: Bool
   let priority: Int

}

public struct variables: Codable {

   let name: String
   let label: String

}

Alamofire 5 Tutorial for iOS: Getting Started, But, how do you work with the JSON data returned? find the Networking group and create a new Swift� Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Did you try this one? This is what it should be

public struct Filter: Codable {
    let id: String
    let name: String
    let properties: Property
}

public struct Property: Codable {
    let variables: [Variable]
}

public struct Variable: Codable {
    let name: String
    let label: String

}

Alamofire Tutorial: Getting Started, Alamofire is a Swift-based HTTP networking library for iOS and macOS. Alamofire provides chainable request/response methods, JSON parameter and In this Alamofire tutorial, you'll use Alamofire to perform basic� I know this question was asked before, but answers were in Swift 3 and using older versions of Alamofire. Problem: Can't figure out how to retrieve data from JSON response, mainly api_key.

The error message you're getting is pretty clear: No value associated with key CodingKeys(stringValue: \"variables\"

You're attempting to decode the JSON into the Filter struct, but the JSON has no variables property. You could fix this by introducing a new Properties struct that will wrap the variables property, like this:

struct Filter: Codable {
    let id: String
    let name: String
    let properties: Properties
}

struct Properties: Codable {
    let variables: Variables
}

struct Variables: Codable {
    let name: String
    let label: String
}

Also, as you'll see in this snippet, it's convention to make the write the type names in CamelCase, so struct Variables instead of struct variables.

Alamofire Tutorial with Swift (Quickstart), If you have been diving deep into iOS development you may have already come across accessing We'll be using Cocoapods to install the Alamofire library. If need to responseJSON { response in debugPrint(response) }. Alamofire.request(.GET, “your url here”).responseJSON { (responseData) -> Void in if((responseData.result.value) != nil) { let swiftyJsonVar = JSON(responseData.result.value!)

SwiftyJSON: How To Parse JSON with Swift, SwiftyJSON is a Swift library for reading and processing JSON data. If you have been diving deep into iOS development you may have already come We will also be using the pod Alamofire 5 to make out http calls easier. Alamofire is a Swift-based HTTP networking library for iOS and macOS. It provides an elegant interface on top of Apple’s Foundation networking stack that simplifies a number of common networking tasks. Alamofire provides chainable request/response methods, JSON parameter and response serialization, authentication, and many other features.

API calls with Alamofire, In this exercise, we'll be dealing with live data from iTunes' top 25 movie feed. Open up ViewController.swift and take a look at the code provided. value = response.result.value { let json = JSON(value) // Do what you need to with JSON here! Alamofire in this project, but it's easy to install in any iOS project - just install� override func viewDidLoad() { super.viewDidLoad() Alamofire.request("http://api.androidhive.info/contacts/").responseJSON { (responseData) -> Void in if((responseData.result.value) != nil) { let swiftyJsonVar = JSON(responseData.result.value!) if let resData = swiftyJsonVar["contacts"].arrayObject { self.arrRes = resData as!

Swift 4 – How to use SwiftyJSON to deal with Alamofire , Let us take an example of the following type of JSON data that you might receive from API Call: Alamofire request and response treatment:� Alamofire is a Swift-based, HTTP networking library. It provides an elegant interface on top of Apple’s Foundation networking stack that simplifies common networking tasks. Its features include chainable request/response methods, JSON and Codable decoding, authentication and more.

Comments
  • You are missing the step "properties" in your JSON before accessing "variables". Either use a custom init, or use a Properties struct between.
  • initially i have properties too but same error came..
  • Tip: When having issue with JSON Codable/Encodable, do the reverse, you'll see what JSON is expecting your code, and can compare with the one you have: pastebin.com/TREcv2zF (based on initial code)
  • still facing same error failure(Alamofire.AFError.responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: Swift.DecodingError.typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "properties", intValue: nil)], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil)))))
  • This will not work because properties is not an array, it's an object. see the JSON carefully.