作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是新来的。如何编写下面的代码并将其转换为 postString,因为我需要提交到 API。
{
"service": 0,
"size": "string",
"orderReference": "string",
"dropOffHubId": "string",
"pickUpHubId": "string",
"courierId": "string",
"from": {
"address": {
"address1": "string",
"address2": "string",
"city": "string",
"postcode": "string",
"state": "string",
"country": "string"
}
},
"to": {
"name": "string",
"phone": "string",
"email": "string",
"address": {
"address1": "string",
"address2": "string",
"city": "string",
"postcode": "string",
"state": "string",
"country": "string"
}
}
}
如果我这样写,我是否正确,但是如何在 "from": { 和 "address": { 之后放入字符串,然后去进入"address1": "string",然后另一个是"to": { "name": "string",然后进去"address": {,然后进入"address1": "string",
let postString = [ "service": 0,
"size": "string",
"orderReference": "string",
"dropOffHubId": "string",
"pickUpHubId": "string",
"courierId": "string",
"from": "fromAddArr",
"to": "toAddArr"] as [String : Any]
var request = URLRequest(url:URL!)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue(bearerToken!, forHTTPHeaderField: "Authorization")
request.httpBody = try! JSONSerialization.data(withJSONObject: postString, options:.prettyPrinted)
我只知道这样写。
{
"refNo": "string",
"code": "string"
}
let postString = ["refNo": "string",
"code": "string"]
最佳答案
试试这个,我想你想要这样的东西:首先从当前的 json 中获取地址字典并将该字典转换为字符串:
let dictionary:[String:Any] = ["address": [
"address1": "string",
"address2": "string",
"city": "string",
"postcode": "string",
"state": "string",
"country": "string"
]
]
if let theJSONData = try? JSONSerialization.data( withJSONObject: dictionary, options: []) {
let theJSONText = String(data: theJSONData, encoding: .utf8)
print("JSON string = \(theJSONText!)") //you can use this string to create dictionary as you want
let dict = ["addres":theJSONText!]
print(dict)
}
关于ios - 如何将帖子字符串写入API swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48942539/
我是一名优秀的程序员,十分优秀!