Alamofire 和 SwiftyJSON 在请求函数之外获取值
Alamofire and SwiftyJSon get value outside request function
嘿,我是新来的,我正在尝试从 VC 中的请求函数之外的请求中获取值,但我做不到,我尝试了几种方法,但总是遇到不同的错误,现在我得到 Type Any 没有下标成员,你能帮我如何从请求中获取字符串并找到一个数组并从中获取值吗?
我需要从 VC 中的 Json 字符串中获取值,所以我正在尝试这种方式:
let retur = Json()
retur.login(userName: userName.text!, password: password.text!) { (JSON) in
print(JSON)
let json = JSON
let name = json["ubus_rpc_session"].stringValue
print(name)
响应:
{"jsonrpc":"2.0","id":1,"result":[0,{"ubus_rpc_session":"70ea230f29057f54459814459b5a316e","timeout":300,"expires":300,"acls":{"access-group":{"superuser":["read","write"],"unauthenticated":["read"]},"ubus":{"":[""],"session":["access","login"]}, "uci":{"*":["read","write"]}},"data":{"username":"root"}}]}
我的要求:
private func makeWebServiceCall (urlAddress: String, requestMethod: HTTPMethod, params:[String:Any], completion: @escaping (_ JSON : Any) -> ()) {
Alamofire.request(urlAddress, method: requestMethod, parameters: params, encoding: JSONEncoding.default).responseString { response in
switch response.result {
case .success:
if let jsonData = response.result.value {
completion(jsonData)
}
case .failure( _):
if let data = response.data {
let json = String(data: data, encoding: String.Encoding.utf8)
completion("Failure Response: \(json)")
}
调用请求方法的函数:
public func login(userName: String, password: String, loginCompletion: @escaping (Any) -> ()) {
let loginrequest = JsonRequests.loginRequest(userName: userName, password: password)
makeWebServiceCall(urlAddress: URL, requestMethod: .post, params: loginrequest, completion: { (JSON : Any) in
loginCompletion(JSON)
})
更新:
您不能使用 Any
进行下标,并且在将 JSON
转换为 [String:Any]
之后,如果您尝试使用 .stringValue
进行下标 Dictionary
然后 Dictionary
没有任何 属性 stringValue
你在这里混合了两个东西 SwiftyJSON
和 Swift 本机类型。我将通过这种方式访问您的 JSON
回复。
首先弄清楚如何从 JSON
响应中获取 ubus_rpc_session
的值。您不能直接从 JSON
响应中获取 ubus_rpc_session
的值,因为它位于 result
数组中的第二个对象内,因此要获取 ubus_rpc_session
尝试这样.
retur.login(userName: userName.text!, password: password.text!) { (json) in
print(json)
if let dic = json as? [String:Any], let result = dic["result"] as? [Any],
let subDic = result.last as? [String:Any],
let session = subDic["ubus_rpc_session"] as? String {
print(session)
}
}
如果您想使用 SwiftyJSON
,那么您可以通过这种方式获得 ubus_rpc_session
的值。
retur.login(userName: userName.text!, password: password.text!) { (json) in
print(json)
let jsonDic = JSON(json)
print(jsonDic["result"][1]["ubus_rpc_session"].stringValue)
}
嘿,我是新来的,我正在尝试从 VC 中的请求函数之外的请求中获取值,但我做不到,我尝试了几种方法,但总是遇到不同的错误,现在我得到 Type Any 没有下标成员,你能帮我如何从请求中获取字符串并找到一个数组并从中获取值吗?
我需要从 VC 中的 Json 字符串中获取值,所以我正在尝试这种方式:
let retur = Json()
retur.login(userName: userName.text!, password: password.text!) { (JSON) in
print(JSON)
let json = JSON
let name = json["ubus_rpc_session"].stringValue
print(name)
响应: {"jsonrpc":"2.0","id":1,"result":[0,{"ubus_rpc_session":"70ea230f29057f54459814459b5a316e","timeout":300,"expires":300,"acls":{"access-group":{"superuser":["read","write"],"unauthenticated":["read"]},"ubus":{"":[""],"session":["access","login"]}, "uci":{"*":["read","write"]}},"data":{"username":"root"}}]}
我的要求:
private func makeWebServiceCall (urlAddress: String, requestMethod: HTTPMethod, params:[String:Any], completion: @escaping (_ JSON : Any) -> ()) {
Alamofire.request(urlAddress, method: requestMethod, parameters: params, encoding: JSONEncoding.default).responseString { response in
switch response.result {
case .success:
if let jsonData = response.result.value {
completion(jsonData)
}
case .failure( _):
if let data = response.data {
let json = String(data: data, encoding: String.Encoding.utf8)
completion("Failure Response: \(json)")
}
调用请求方法的函数:
public func login(userName: String, password: String, loginCompletion: @escaping (Any) -> ()) {
let loginrequest = JsonRequests.loginRequest(userName: userName, password: password)
makeWebServiceCall(urlAddress: URL, requestMethod: .post, params: loginrequest, completion: { (JSON : Any) in
loginCompletion(JSON)
})
更新:
您不能使用 Any
进行下标,并且在将 JSON
转换为 [String:Any]
之后,如果您尝试使用 .stringValue
进行下标 Dictionary
然后 Dictionary
没有任何 属性 stringValue
你在这里混合了两个东西 SwiftyJSON
和 Swift 本机类型。我将通过这种方式访问您的 JSON
回复。
首先弄清楚如何从 JSON
响应中获取 ubus_rpc_session
的值。您不能直接从 JSON
响应中获取 ubus_rpc_session
的值,因为它位于 result
数组中的第二个对象内,因此要获取 ubus_rpc_session
尝试这样.
retur.login(userName: userName.text!, password: password.text!) { (json) in
print(json)
if let dic = json as? [String:Any], let result = dic["result"] as? [Any],
let subDic = result.last as? [String:Any],
let session = subDic["ubus_rpc_session"] as? String {
print(session)
}
}
如果您想使用 SwiftyJSON
,那么您可以通过这种方式获得 ubus_rpc_session
的值。
retur.login(userName: userName.text!, password: password.text!) { (json) in
print(json)
let jsonDic = JSON(json)
print(jsonDic["result"][1]["ubus_rpc_session"].stringValue)
}