验证json数据,它不需要用户名和密码吗?你能纠正我的代码吗?
Posted
技术标签:
【中文标题】验证json数据,它不需要用户名和密码吗?你能纠正我的代码吗?【英文标题】:authenticating json data, it does not take username and password in swift? can you correct me my code? 【发布时间】:2017-05-23 11:13:30 【问题描述】:http://beta.json-generator.com/api/json/get/E1jKVbplX我已经注册成功了。我对身份验证感到震惊。我访问了所有数据。但我对访问个人用户数据感到震惊。请参阅下面的代码并纠正我。 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()
【问题讨论】:
myJson
是Array
。那么为什么myJson["Email"]
应该工作呢?那是为了一个字典。 myJson[0]["Email"]
而不是?
【参考方案1】:
您的响应是字典数组,因此myJson[0]
将返回一个字典对象,在该字典中您应该尝试访问Email
或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("--------")
注意:你需要即兴发挥。
【讨论】:
以上是关于验证json数据,它不需要用户名和密码吗?你能纠正我的代码吗?的主要内容,如果未能解决你的问题,请参考以下文章
你能计算出 Active Directory 使用的密码哈希吗?
CTF---安全杂项入门第三题 这是捕获的黑客攻击数据包,Administrator用户的密码在此次攻击中泄露了,你能找到吗?