[英]authenticating json data, it does not take username and password in swift? can you correct me my code?
http://beta.json-generator.com/api/json/get/E1jKVbplX I have registered successfully. http://beta.json-generator.com/api/json/get/E1jKVbplX我已经成功注册。 i am struck in authentication. 我在身份验证中感到震惊。 i accessed all data. 我访问了所有数据。 but i struck in accessing individual user data. 但是我很喜欢访问个人用户数据。 please see below code and correct me. 请查看下面的代码并纠正我。 var email = “bhupal” var pwd = “k” var email =“ bhupal” var pwd =“ k”
//
let parameters: Dictionary<String, AnyObject> = ["Email": email as AnyObject, "Password": pwd as AnyObject,"Type" : "Organization" as AnyObject,"Mode" : "Register" as AnyObject]
//create the url with URL
let url = URL(string: "http://beta.json-generator.com/api/json/get/E1jKVbplX ")! //change the url
let session = URLSession.shared
//now create the URLRequest object using the url object
var request = URLRequest(url: url)
request.httpMethod = "GET" //set http method as GET
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.localizedDescription)
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
//
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil
{
print ("ERROR")
}
else
{
if let content = data
{
do
{
//Array
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
print(myJson)
print("--------")
print(myJson[0])
print(myJson[1])
print("--------")
email = myJson["Email"] as? String
pwd = myJson["Password"] as? String
print("--------")
print(email)
print(pwd)
print("--------")
}
catch
{
}
}
}
}
task.resume()
Your response is Array of Dictionary so myJson[0]
will return you a dictionary object and in that dictionary you should try accessing value for Email
or Password
. 您的响应是字典数组,因此myJson[0]
将返回一个字典对象,在该字典中,您应该尝试访问Email
或Password
值。 You can try below code to access email and password : 您可以尝试以下代码来访问电子邮件和密码:
for dict in myJson { // myJson is array and dict will of dictionary object
let email = dict[Email]
let password = dict[Password]
print("--------")
print("Email is : \(email)")
print("Password is : \(password)")
print("--------")
}
Note : You make need to improvise it. 注意 :您需要即兴创作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.