gpt4 book ai didi

json - 如何使用 SWIFT 通过 JSON 将数据从多个 View Controller POST 到服务器

转载 作者:行者123 更新时间:2023-11-30 11:16:23 26 4
gpt4 key购买 nike

我需要帮助来组合从firstVC、secondVC和thirdVC收集的数据并将这些数据序列化到fourthVC中。

此链接有助于一个 VC,但我只需将一个 JSON 数据文件发送到服务器。

How to create and send the json data to server using swift language

另一种方法是将字典数组从firstVC、secondVC和thirdVC传递到fourthVC,并从fourthVC将字典转换为JSON。但我不知道该怎么做。

我使用了上面链接中提供的答案中的格式,但如果您需要其他信息,我将很乐意合作。谢谢!

PS。请给我有用的评论,以任何方式提供帮助。我需要代码,而不是像做我自己的研究这样的反馈,因为我已经为此强调了近一个月了。

这是 UserDefault 键

 if let AC = UserDefaults.standard.value(forKey: "Acc") as? String {
labeltext.text = "\(AC)"
}

if let TY = UserDefaults.standard.value(forKey: "Taxyear") as? String {
taxtext.text = "\(TY)"
}
if let BB = UserDefaults.standard.value(forKey: "Bsb") as? String {
bsbtext.text = "\(BB)"
}

这是我的 JSON 代码

@IBAction func save(_ sender: Any){
typealias JSONDictionary = [String:Any]

let parameters = ["BankAccountNumber": "Acc", "Tax Year": "Taxyear", "my-bsb": "Bsb"]

let url = URL(string: "https://server:port/")! //change the url

//create the session object
let session = URLSession.shared

//now create the URLRequest object using the url object
var request = URLRequest(url: url)
request.httpMethod = "POST" //set http method as POST


let valid = JSONSerialization.isValidJSONObject(parameters) // true

print (valid)

do {
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) // pass dictionary to nsdata object and set it as request body

} catch let error {
print(error)
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

// create dataTask using the session object to send data to the server
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in

guard error == nil else {
return
}
guard let data = data else {
return
}

do {
//create json object from data
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
print(json)
// handle json...
}

} catch let error {
print(error.localizedDescription)
}
})
task.resume()

let alertMessage = UIAlertController(title: "Saved!", message: "We have recorded your information", preferredStyle: UIAlertControllerStyle.alert)

let action = UIAlertAction(title:"Okay", style: UIAlertActionStyle.default, handler: nil)

alertMessage.addAction(action)

self.present(alertMessage, animated: true, completion: nil)

}

最佳答案

我通过首先将它们存储在变量中来解决这个问题

var TITLE = UserDefaults.standard.value(forKey: "Title")
var GN = UserDefaults.standard.value(forKey: "GivenNames")
var LN = UserDefaults.standard.value(forKey: "LastName")

然后我将它们放入一个参数中,就完成了。这是如此明显,我不敢相信我没有早点解决它

@IBAction func save(_ sender: Any){

let parameters = ["Tax Year": TaxYear, "Title": TITLE, "first-name": GN, "sur-name": LN]

关于json - 如何使用 SWIFT 通过 JSON 将数据从多个 View Controller POST 到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51700919/

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