How do I use JSON arrays with Alamofire parameters?

前端 未结 8 1907
借酒劲吻你
借酒劲吻你 2021-01-05 01:03

I\'m having a bit of trouble structuring my parameters so that our server API would be able to read it as valid JSON.

Alamofire uses parameters like this in swift la

相关标签:
8条回答
  • 2021-01-05 01:14

    if you are using SwiftyJSON, you can write like this

    let array = ["2010-12-13T5:03:20","2010-12-13T5:03:20"]
     let paramsJSON = JSON(array)
     var arrString = paramsJSON.rawString(NSUTF8StringEncoding)
    
    0 讨论(0)
  • 2021-01-05 01:16

    You need to create a NSArray object for array parameters.

    var yourParameters = [
      "String": "a string",
      "Int": 1,
      "Array": NSArray(
        array: [
            "a", "b", "c"
        ])
    ]
    
    0 讨论(0)
  • 2021-01-05 01:19

    Swift 2.2 and using SwiftyJSON.swift
    You can use like this.

        var arrayList : [String: AnyObject]//one item of array
    
        var list: [JSON] = []//data array 
    
        for i in 0..<10//loop if you need
        { 
    
            arrayList = [
                "param1":"",
                "param1":"",
                "param2":["","",""]
            ]
    
            list.append(JSON(arrayList))//append to your list
    
        }
    
    
        //params
        let params: [String : AnyObject]=[
    
            "Id":"3456291",
            "List":"\(list)"//set
        ]
    
    0 讨论(0)
  • 2021-01-05 01:22

    In case, there is a need to pass array directly as a parameter for a alamofire request, the following method worked for me.

    source: https://github.com/Alamofire/Alamofire/issues/1508

    let headers = NetworkManager.sharedInstance.headers
    var urlRequest = URLRequest(url: URL(string: (ServerURL + Api))!)
    urlRequest.httpMethod = "post"
    urlRequest.allHTTPHeaderFields = headers
    let jsonArrayencoding = JSONDocumentArrayEncoding(array: documents)
    let jsonAryEncodedRequest = try? jsonArrayencoding.encode(urlRequest, with: nil)
    
        var request: Alamofire.DataRequest? = customAlamofireManager.request(jsonAryEncodedRequest!)
        request?.validate{request, response, data in
            return .success
            }
            .responseJSON {
    
    0 讨论(0)
  • 2021-01-05 01:27

    Using Swift 5 You can use like this.

      var authParams:[String:Any] = [:]
    
      var authParamsObject:[String:Any] = [:]
    
    
        authParamsObject["is_urgent"] = data_any
        authParamsObject["body"] = data_any
    
      authParams = ["note_set" : authParamsObject]
    

    Json Result :

    {
      "note_set":
        {
          "is_urgent": true,
          "body": "string"
        }
    }
    
    0 讨论(0)
  • 2021-01-05 01:28

    In my case.

    let query = "blm"
    let allCountriesParameter = ["country": "*"]
    let jsonObject: [String: Any] = ["query": query, "locations": [allCountriesParameter]]
    let httpBody = try JSONSerialization.data(withJSONObject: jsonObject)
    

    How it looks.

        print(String(data: httpBody, encoding: .utf8))
    
        {
           "query":"blm",
           "locations":[
              {
                 "country":"*"
              }
           ]
        }
    
    0 讨论(0)
提交回复
热议问题