gpt4 book ai didi

ios - 如何将帖子字符串写入API swift 3

转载 作者:行者123 更新时间:2023-11-30 11:45:01 24 4
gpt4 key购买 nike

我是新来的。如何编写下面的代码并将其转换为 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com