json - Swift:从 php 解码 NSString
问题描述
我有以下代码,我无法将 responseString 拆分/解码为多个部分。我希望能够将其转换为我可以在整个程序中使用的变量。(第一次发帖,感谢您的耐心等待)。我无法让 responseString 在 {} 括号外工作,也无法在括号内进行 JSON 解码。
@IBAction func onLogin(_ sender: Any) {
let loginUserSend = loginUser.text
let loginPasswordSend = loginPassword.text
if loginUserSend!.isEmpty {
loginError.text = "Please input user name"
loginError.textColor = UIColor.red
} else if loginPasswordSend!.isEmpty {
loginError.text = "Please input password"
loginError.textColor = UIColor.red
} else {
print ("Moving to php request:")
let request = NSMutableURLRequest(url: NSURL(string: "http://www.website.com/IOS/connectionIOSlogin.php")! as URL)
request.httpMethod = "POST"
print("Request: \(request)")
let postString = "loginUser=\(loginUser.text!) "
request.httpBody = postString.data(using: String.Encoding.utf8)
print("postString: \(postString)")
let task = URLSession.shared.dataTask(with: request as URLRequest) {
(data, response, error) in
if error != nil {
print("error=\(error)")
return
} // Ends errror If statements
//print("response = \(response)")
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("responseString = \(responseString)")
}
task.resume()
它返回以下内容:
Moving to php request:
Request: <NSMutableURLRequest: 0x600002bdc180> { URL: http://www.website.com/IOS/connectionIOSlogin.php }
postString: loginUser=Test1
2020-09-22 20:34:55.471811-0300 ABCDEF GHIJK[17260:1385975] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed
responseString = Optional(Row :Array
(
[0] => 1
[C_Id] => 1
[1] => Test1
[C_User] => Test1
[2] => Test1
[C_Pword] => Test1
)
Array
(
[0] => 1
[C_Id] => 1
[1] => Test1
[C_User] => Test1
[2] => Test1
[C_Pword] => Test1
)
)
解决方案
推荐阅读
- ssl - 证书文件的正确内容
- outlook - 当事件来自 Microsoft Graph 上的邀请时,日历事件的类别数组为空
- python - 保存的 keras 模型在推理时冻结内核
- javascript - javascript条件更新内部部分属性而不删除其他属性
- build - Visual Studio Code 上的“找不到要运行的构建任务”错误
- bash - 从进程主动写入的bash内联中的文件开头删除前N个字节/行
- sql - 如何将丢失的元数据合并到 SQL 语句中?
- amazon-web-services - 为什么 AWS 控制台中有两个“无限制更改 T2”选项?
- python - (effeciently) Iterating recursively over multiple dataframes in pandas
- xml - 如何在 Taleo Connect 客户端中使用常量字符串作为投影?