How do I use JSON arrays with Alamofire parameters?

前端 未结 8 1996
借酒劲吻你
借酒劲吻你 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条回答
  •  猫巷女王i
    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":"*"
              }
           ]
        }
    

提交回复
热议问题