json - Alamofire 和 SwiftyJSON 在请求函数之外获取值

标签 json swift xcode swifty-json

嘿,我是新来的,我试图从 VC 中的请求函数之外的请求中获取值,但我做不到,我尝试了几种方法时遇到错误,但我不断收到不同的错误,现在我得到了 Type Any 有没有下标成员,你能帮我如何从请求中获取字符串并找到一个数组并从中取值吗?

我需要从 VC 中的 Json strin 获取值,所以我正在尝试这种方式:

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)
})

更新: enter image description here enter image description here

最佳答案

如果您正在尝试 ,则不能使用 Any 下标,并且在将 JSON 转换为 [String:Any] 之后。带有下标 Dictionary 的 stringValue 然后 Dictionary 没有任何属性 stringValue 你在这里混合了两个东西 SwiftyJSON 和 Swift native 类型。我将通过这种方式访问​​您的 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)
}

关于json - Alamofire 和 SwiftyJSON 在请求函数之外获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41996796/

相关文章:

javascript - 将数据库中的数据格式化为 Highcharts 系列格式

python - 如何将复杂的json字符串反序列化为python对象?

ios - 旧版本的 Xcode 仍在/Developer/Applications - 我可以完全删除/Developer 吗?

XCode:堆栈 View 和约束

xcode - 访问加载到项目中的文件时遇到问题

c++ - Xcode 3.2 : C++ compilation errors 的 QuickTime 组件

javascript - NodeJS Express - 返回它触发的 URL 路径的 JSON 文件

javascript - Angular JS 中的多级 json 过滤器

ios - 如何快速将 NSObject 值转换为 String?

swift - 如何在 swift 中将字符串转换为 char* ?