[英]Create a Json in Swift 5
I am new in swift and I want to make a post request with AlamoFire 5.4 and Swift 5我是 swift 的新手,我想用 AlamoFire 5.4 和 Swift 5 发出帖子请求
This is the object that I need to send to the server and I don't know how to create its equivalent in swift这是我需要发送到服务器的对象,我不知道如何在 swift 中创建它的等价物
[
{
"KEY": "LT_APP",
"VALUE":"[{\"P_TIPO\":\"L\",\"P_PERNR\":\"925\",\"P_PASS\":\"GAMEROS01\",\"P_CEL\":\"6143194524\",\"P_TOKEN\":\"asdfgh\"}]"
}
]
The content inside value is a string value 里面的内容是一个字符串
In Postman looks like this在邮递员看起来像这样
enter image description here在此处输入图片说明
This is what I have这就是我所拥有的
let jsonObject // Here is my problem xD how to build the object
AF.request(url,
method: .post,parameters: jsonObject , encoding: JSONEncoding.default)
.authenticate(username: user, password: password)
.responseJSON { response in
switch response.result {
case .success(let json):
let rtn = JSON(json)
print(rtn["result"]["RESPONSE"][0])
case .failure(let error):
print(error)
}
}
I tried several ways to create it, inside a class, with a [String: Any] dictionary and finally the object declared directly我尝试了几种方法来创建它,在一个类中,使用 [String: Any] 字典,最后直接声明的对象
class Valores: NSObject{
var KEY:String
var VALUE:String
init(key: String, value: String){
self.KEY = key
self.VALUE = value
}
}
var Arreglo = [Valores] = [Valores]()
let objeto : Valores = Valores(key: "LT_APP", value:"[{\"P_TIPO\":\"L\",\"P_PERNR\":\"925\",\"P_PASS\":\"GAMEROS01\",\"P_CEL\":\"6143194524\",\"P_TOKEN\":\"asdfgh\"}]")
Arreglo.append(Objeto)
Thanks谢谢
not sure its right way to do but may be this will work ..不确定它的正确方法,但可能会起作用..
let arr = [["P_TIPO":"L"],["P_PERNR":"925"],["P_PASS":"GAMEROS01"],["P_CEL":"6143194524"],["P_TOKEN":"asdfgh"]]
var text = "\(arr)"
text = text.replacingOccurrences(of: "[", with: "", options: NSString.CompareOptions.literal, range:nil)
text = text.replacingOccurrences(of: "]", with: "", options: NSString.CompareOptions.literal, range:nil)
text = "[{\(text)}]"
let rest = [["KEY": "LT_APP"], ["VALUE": "\(text)"]]
print(rest)
i think you are little bit confused here.我想你在这里有点困惑。 so, i explain some points.
所以,我解释一些要点。
dictionary array
in parameter
as you share in image.so, declare an array of dictionaryparameter
传递一个dictionary array
,因为你在image.so中共享,声明一个字典数组var param = [String:Any] //here is some problem i can't input dictionary array so see in snippet.
var param = [String:Any] //这里有一些问题,我无法输入字典数组,请参见代码片段。
[{\\"P_TIPO\\":\\"L\\",\\"P_PERNR\\":\\"925\\",\\"P_PASS\\":\\"GAMEROS01\\",\\"P_CEL\\":\\"6143194524\\",\\"P_TOKEN\\":\\"asdfgh\\"}]
so, first take dictionary[{\\"P_TIPO\\":\\"L\\",\\"P_PERNR\\":\\"925\\",\\"P_PASS\\":\\"GAMEROS01\\",\\"P_CEL\\":\\"6143194524\\",\\"P_TOKEN\\":\\"asdfgh\\"}]
所以,先取字典let dict = ["P_TIPO":"L", "P_PERNR":"925", "P_PASS":"GAMEROS01", "P_CEL":"6143194524", "P_TOKEN":"asdfgh"]
let dict = ["P_TIPO":"L", "P_PERNR":"925", "P_PASS":"GAMEROS01", "P_CEL":"6143194524", "P_TOKEN":"asdfgh"]
and than put that dictionary in array然后将该字典放入数组
let arrVal = [dict]
让 arrVal = [dict]
param = [["LT_APP":arrVal]]
参数 = [["LT_APP":arrVal]]
see the snippet code查看代码段
var param = [[String:Any]]() let dict = ["P_TIPO":"L", "P_PERNR":"925", "P_PASS":"GAMEROS01", "P_CEL":"6143194524", "P_TOKEN":"asdfgh"] let arrVal = [dict] param = [["LT_APP":arrVal]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.