Ios 在url请求中传递嵌套字典问题

Ios 在url请求中传递嵌套字典问题,ios,arrays,json,swift,dictionary,Ios,Arrays,Json,Swift,Dictionary,我正在尝试使用URL请求将嵌套字典传递给服务器 我尝试以下字典格式: var pdictData = ["post_id":"JobListObj.JobID","comment":txtMsg.text,"rating[price]":viewPrice.rating,"rating[service]":viewService.rating,"rating[location]"

我正在尝试使用URL请求将嵌套字典传递给服务器

我尝试以下字典格式:

var pdictData = ["post_id":"JobListObj.JobID","comment":txtMsg.text,"rating[price]":viewPrice.rating,"rating[service]":viewService.rating,"rating[location]":viewLocation.rating,"rating[quality]":viewQuality.rating] as [String : Any]
它根本不起作用。请把评级数据传给我

以下是我的web api开发人员建议的格式:

post_id:1830
comment:Rateing again
rating[quality]:1
rating[location]:1
rating[service]:1
rating[price]:1

这种格式对我来说很奇怪,也许你们应该在请求体上传递数据,而不是url参数。尝试分享有关您的问题的API文档(如果是公开的)

我猜您希望使用post方法发送数据。所以,您需要创建一个这样的结构

struct Job: Codable {
   let post_id: Int
   let comment: String 
   let rating: Rating  
}

struct Rating: Codable { 
   let quality: Int
   let location: Int
   let service: Int
   let price: Int
}
然后配置您的请求:

let job = Job( .... )
let data = try? JSONEncoder().encode(job)
let url = URL(string: "MY_HOST")
var request = URLRequest(url: url)
request.httpBody = data

参数应该是URL编码的?另外,你能发布一个“让我们说邮递员”的工作示例吗?@matt尝试使用rating dictionary对象不工作,谢谢。@gcharita,谢谢它使用url编码使用相同的参数结构为我工作。如果问题解决了,请回答你自己的问题,或者删除它。谢谢