在我的 swift 上,我從 json 中收到來自我的 php 的資料。
當我列印它時,它直接顯示所有我想知道是否有辦法將這些資料直接放在表格中,或者我是否必須洗掉我不想要的字符并剪切我的字串等。
這是我的代碼
let parameters = ["id":"\(id)","password":"\(password)"]
var sParams = ""
for (key,value) in parameters{
sParams = key "=" (value as String) "&"
print("\(key),\(value as String)")
}
if !sParams.isEmpty{
sParams = "?" sParams
if sParams.hasSuffix("&"){
sParams.removeLast()
}
urlLink = urlLink sParams
}else{
print("!sParams.isEmpty fail")
}
let serializer = DataResponseSerializer(emptyResponseCodes: [200,204,205])
var request = URLRequest(url : URL(string:urlLink)!)
AF.request(request).uploadProgress{progress in }.response(responseSerializer: serializer){ response in
if response.error == nil {
var responseString: String!
responseString = ""
if response.data != nil {
responseString = String(bytes:response.data!, encoding: .utf8)
}else{
responseString = response.response?.description
}
var dataJson = responseString ?? ""
dataJson = dataJson.replacingOccurrences(of: "\"", with: "")
dataJson = dataJson.replacingOccurrences(of: "password:", with: "")
dataJson = dataJson.replacingOccurrences(of: "email:", with: "")
dataJson = dataJson.replacingOccurrences(of: "{", with: "")
dataJson = dataJson.replacingOccurrences(of: "}", with: "")
dataJson = dataJson.replacingOccurrences(of: "[", with: "")
dataJson = dataJson.replacingOccurrences(of: "]", with: "")
print(dataJson)
var finalData = dataJson.components(separatedBy: ",")
if finalData[0] == id as String && finalData[1] == password as String{
self.present(HomeClientViewController(), animated: true)
}else{
print("erreur dans la conversion")
}
// print(responseString ?? "")
// print(response.response?.statusCode as Any)
var data : NSData!
if let data = data {
//Succeed
let dataLength = data.length
print("Size: \(dataLength) Bytes")
}else{
//print(data)
}
print("response time: \(response.metrics?.taskInterval.duration ?? 0)")
//self.present(HomeClientViewController(), animated: true)
}
}
這是我如何接收資料的日志:
[{"email":"[email protected]","password":"1234"}]
uj5u.com熱心網友回復:
是的,我嘗試過搜索,但沒有找到好的答案,所以我嘗試了最長的方法
dataJson = dataJson.replacingOccurrences(of: "\"", with: "")
dataJson = dataJson.replacingOccurrences(of: "password:", with: "")
dataJson = dataJson.replacingOccurrences(of: "email:", with: "")
dataJson = dataJson.replacingOccurrences(of: "{", with: "")
dataJson = dataJson.replacingOccurrences(of: "}", with: "")
dataJson = dataJson.replacingOccurrences(of: "[", with: "")
dataJson = dataJson.replacingOccurrences(of: "]", with: "")
var finalData = dataJson.components(separatedBy: ",")
這樣我可以將我的資料放入表格中,但我發現這樣做并不干凈
uj5u.com熱心網友回復:
首先解碼json:
struct User: Decodable {
let email: String
let password: String
}
let jsonFormatter = JSONDecoder()
if let jsonData = jsonString.data(using: .utf8) {
let user = try jsonFormatter.decode([User].self, from: jsonData).first
}
然后,使用用戶項填充您喜歡的視圖。
uj5u.com熱心網友回復:
謝謝你所有的回答,所以我嘗試使用 Decodable 這里是我用來正確解碼我的 JsonData 的代碼:
struct Login: Decodable {
let email: String
let password: String
}
let jsonObjectData = dataJson!.data(using: .utf8)!
let login = try? JSONDecoder().decode(
Login.self,
from: jsonObjectData
)
print(login!.email as Any)
print(login!.password as Any)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/534956.html
標籤:PHPJSON迅速字典
上一篇:如何使用Swift5.7RegexBuilder在句子后查找單詞
下一篇:如何快速將小時轉換為秒?[復制]
