如何将 poststring 写入 API swift3
How to write poststring to API swift3
我是新来的。如何编写下面的代码并将其转换为 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)
}
你不需要自己写那个字符串。如果您使用的是 swift 3 并且该 JSON 字符串有一个模型 class,您可以使用第三方库 here。这会将您的模型转换为字典、数组、json 字符串和数据等。您只需要使模型成为 ParsableModel
或 JSONParsable
的子模型。
如果您在从 JSON 创建模型 class 时遇到问题,您可以使用 this 实用程序。这将为您的 JSON:
生成模型 class
class RootClass {
var courierId: String?
var dropOffHubId: String?
var from: From?
var orderReference: String?
var pickUpHubId: String?
var service: NSNumber?
var size: String?
var to: To?
}
class To {
var address: Addres?
var email: String?
var name: String?
var phone: String?
}
class From {
var address: Addres?
}
class Addres {
var address1: String?
var address2: String?
var city: String?
var country: String?
var postcode: String?
var state: String?
}
如果您正在使用 Swift 4. 它为您提供 classes 来编码和解码您的代码,从 JSON 建模和建模到 JSON。使用这个可以节省您的时间。
我是新来的。如何编写下面的代码并将其转换为 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)
}
你不需要自己写那个字符串。如果您使用的是 swift 3 并且该 JSON 字符串有一个模型 class,您可以使用第三方库 here。这会将您的模型转换为字典、数组、json 字符串和数据等。您只需要使模型成为 ParsableModel
或 JSONParsable
的子模型。
如果您在从 JSON 创建模型 class 时遇到问题,您可以使用 this 实用程序。这将为您的 JSON:
生成模型 classclass RootClass {
var courierId: String?
var dropOffHubId: String?
var from: From?
var orderReference: String?
var pickUpHubId: String?
var service: NSNumber?
var size: String?
var to: To?
}
class To {
var address: Addres?
var email: String?
var name: String?
var phone: String?
}
class From {
var address: Addres?
}
class Addres {
var address1: String?
var address2: String?
var city: String?
var country: String?
var postcode: String?
var state: String?
}
如果您正在使用 Swift 4. 它为您提供 classes 来编码和解码您的代码,从 JSON 建模和建模到 JSON。使用这个可以节省您的时间。