使用 alamofire 处理 Json 响应
Handling Json response with alamofire
我正在开发一个 IOS 登录应用程序,但我不知道如何处理来自服务器的 Json 响应,我想编写一个依赖于服务器响应:
如果用户名和密码正确,这是服务器响应:
SUCCESS: {
users = (
{
email = test;
id = 1;
money = 200;
password = test;
username = test;
}
);
}
如果用户名和密码错误:
SUCCESS: {
users = (
);
}
这是我用 NodeJs 编写的后端代码:
app.get('/login/:username/:password',(req,res)=>{
let user = req.params;
var sql = "SELECT * FROM users WHERE username = ? And password = ? ";
mysqlConnection.query(sql,[user.username,user.password],
function(err, rows){
if(!err){
res.send(JSON.stringify({"users" : rows}));
}
else {
console.log(err)
}
}
这是我的 swift 功能:
class func login(username : String , password : String, _ completion: @escaping (Bool) -> ()) {
let url = "http://127.0.0.1:3000/login/"+username+"/"+password
Alamofire.request(url).responseJSON{response in
switch response.result
{
case .failure:
print(response)
completion(false)
case .success:
//I want to handle the response here
//return true if the username and password are right
//return wrong if not
print(response)
completion(true)
}
}
}
在这两种情况下,您都会得到 SUCCESS
,因此您可以将 users
作为密钥,并检查它是否包含任何元素,如果用户名和密码正确,您将得到
(
{
email = test;
id = 1;
money = 200;
password = test;
username = test;
}
)
但如果用户名和密码错误,您将得到 users
键的空值,因此在这种情况下您可以使用
if usersDict.count == 0 {
//username and password are wrong
} else {
//username and the password are right
}
使用上面的代码:-
func CallAPI(){
let parameters: [String: Any] = [
"Username": "Admin",
"Password": "123456",
"Language_Code": "EN"]
Alamofire.request("Your API Url", method: .post, parameters: parameters, encoding: JSONEncoding.default)
.responseJSON { response in
if((response.result.value) != nil) {
let ResultJson = JSON(response.result.value!)
print("ResultJson==",ResultJson)
let UserCount = ResultJson["users"].count
if UserCount > 0 {
// Do with your above code
let Email = ResultJson["users"]["email"].stringValue
let id = ResultJson["users"]["id"].intValue
let money = ResultJson["users"]["money"].intValue
let password = ResultJson["users"]["password"].stringValue
let username = ResultJson["users"]["username"].stringValue
}
}
}
}
我正在开发一个 IOS 登录应用程序,但我不知道如何处理来自服务器的 Json 响应,我想编写一个依赖于服务器响应: 如果用户名和密码正确,这是服务器响应:
SUCCESS: {
users = (
{
email = test;
id = 1;
money = 200;
password = test;
username = test;
}
);
}
如果用户名和密码错误:
SUCCESS: {
users = (
);
}
这是我用 NodeJs 编写的后端代码:
app.get('/login/:username/:password',(req,res)=>{
let user = req.params;
var sql = "SELECT * FROM users WHERE username = ? And password = ? ";
mysqlConnection.query(sql,[user.username,user.password],
function(err, rows){
if(!err){
res.send(JSON.stringify({"users" : rows}));
}
else {
console.log(err)
}
}
这是我的 swift 功能:
class func login(username : String , password : String, _ completion: @escaping (Bool) -> ()) {
let url = "http://127.0.0.1:3000/login/"+username+"/"+password
Alamofire.request(url).responseJSON{response in
switch response.result
{
case .failure:
print(response)
completion(false)
case .success:
//I want to handle the response here
//return true if the username and password are right
//return wrong if not
print(response)
completion(true)
}
}
}
在这两种情况下,您都会得到 SUCCESS
,因此您可以将 users
作为密钥,并检查它是否包含任何元素,如果用户名和密码正确,您将得到
(
{
email = test;
id = 1;
money = 200;
password = test;
username = test;
}
)
但如果用户名和密码错误,您将得到 users
键的空值,因此在这种情况下您可以使用
if usersDict.count == 0 {
//username and password are wrong
} else {
//username and the password are right
}
使用上面的代码:-
func CallAPI(){
let parameters: [String: Any] = [
"Username": "Admin",
"Password": "123456",
"Language_Code": "EN"]
Alamofire.request("Your API Url", method: .post, parameters: parameters, encoding: JSONEncoding.default)
.responseJSON { response in
if((response.result.value) != nil) {
let ResultJson = JSON(response.result.value!)
print("ResultJson==",ResultJson)
let UserCount = ResultJson["users"].count
if UserCount > 0 {
// Do with your above code
let Email = ResultJson["users"]["email"].stringValue
let id = ResultJson["users"]["id"].intValue
let money = ResultJson["users"]["money"].intValue
let password = ResultJson["users"]["password"].stringValue
let username = ResultJson["users"]["username"].stringValue
}
}
}
}