我需要为我的应用程序提供付款方式,并且必须发布带有JSON数据的请求才能与API进行通信。一切对我来说似乎都是正确的。我在代码中找不到任何错误,但是我认为JSON没有按顺序发布。这重要吗?因为响应表示失败,但我找不到其他东西。如果JSON顺序很重要,我该怎么做?我是新手,请帮助我。这是我的代码:
func mainRequestForPayment() {
)
let headers: HTTPHeaders = [
"accept": "application/json",
"content-type": "application/json",
"authorization": "\(self.authValue)",
"x-iyzi-rnd": "\(self.randomString)",
"cache-control": "no-cache"
]
let url = "MY_URL"
let parameters: [String: Any] = [
"locale": "tr",
"conversationId": "123456789",
"price": "1.1",
"paidPrice": "1.1",
"installment": 1,
"paymentChannel": "WEB",
"basketId": "B67832",
"paymentGroup": "PRODUCT",
"paymentCard": [
"cardHolderName": "CARD_HOLDER_NAME",
"cardNumber": "CARD_NUMBER",
"expireYear": "CARD_YEAR",
"expireMonth": "01",
"cvc": "123",
"registerCard": 0
],
"buyer": [
"id": "BY789",
"name": "John",
"surname": "Doe",
"identityNumber": "74300864791",
"email": "[email protected]",
"gsmNumber": "+905350000000",
"registrationAddress": "Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1",
"city": "Istanbul",
"country": "Turkey",
"zipCode": "34732",
"ip": "85.34.78.112"
],
"shippingAddress": [
"address": "Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1",
"zipCode": "34742",
"contactName": "Jane Doe",
"city": "Istanbul",
"country": "Turkey"
],
"billingAddress": [
"address": "Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1",
"zipCode": "34742",
"contactName": "Jane Doe",
"city": "Istanbul",
"country": "Turkey"
],
"basketItems": [
[
"id": "BI101",
"price": "0.3",
"name": "Binocular",
"category1": "Collectibles",
"category2": "Accessories",
"itemType": "PHYSICAL"
],
[
"id": "BI102",
"price": "0.5",
"name": "Game code",
"category1": "Game",
"category2": "Online Game Items",
"itemType": "VIRTUAL"
],
[
"id": "BI103",
"price": "0.2",
"name": "Usb",
"category1": "Electronics",
"category2": "Usb / Cable",
"itemType": "PHYSICAL"
]
],
"currency": "TRY"
]
Alamofire.request(url, method: .post, parameters: parameters , encoding: JSONEncoding.default, headers: headers)
.responseJSON { (response) in
print(parameters)
switch response.result {
case .success(let value):
let swiftyJson = JSON(value)
print ("return as JSON using swiftyJson is: \(swiftyJson)")
case .failure(let error):
print ("error: \(error)")
}
}
}
我看不到我的错在哪里?再有什么办法可以在发布请求中下订单?谢谢大家
我得到那个回应:
return as JSON using swiftyJson is: {
"conversationId" : "123456789",
"locale" : "tr",
"errorCode" : "1000",
"status" : "failure",
"systemTime" : 1579858355103,
"errorMessage" : "Invalid signature"
}
JSON顺序通常并不重要,因为JSON规范并未将其定义为JSON对象的要求,但是某些设计欠佳的后端确实需要它。您确实需要检查与之通信的后端的要求。
此外,Swift的Dictionary
类型是任意排序的,并且该顺序可能在应用程序运行之间以及用于编译代码的Swift版本之间改变。
最后,Swift的JSONEncoder
和Apple的JSONSerialization
类型都无法提供要求严格订购的方法。最多JSONSerialization
提供.sortedKeys
选项,这将为您提供有保证的(字母顺序)顺序,但可能不是您在其中声明参数的顺序。Encoder
如果您有Codable
类型(我建议使用它而不是SwiftyJSON),则使用Alternative可以给出您可以更好地保证订单秩序,但是只有在后端需要时,才应该真正在乎。
顺便说一句,我建议您使用静态HTTPHeader
属性作为HTTPHeaders
值,而不是使用原始字符串,这样会更加方便。例如:
let headers: HTTPHeaders = [.accept("application/json"),
.contentType("application/json")]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句