简体   繁体   English

Swift 3 Alamofire swiftyjson下标

[英]Swift 3 alamofire swiftyjson subscript

I don't know why my code doesn't work, my result is always nil. 我不知道为什么我的代码不起作用,我的结果总是为零。 I integrated alamofire and swiftyjson this is my code: 我集成了alamofire和swiftyjson,这是我的代码:

let urlString = "myurl"

let params: Parameters = [
    "accessProvider": AccessProvider,
    "inputToken": AccessToken
]


Alamofire.request(urlString, method: .post, parameters: params, encoding: URLEncoding.httpBody)
    .responseJSON { response in
        if let responseObject = response.result.value {
            print("JSON: \(responseObject)")

            let json = JSON(responseObject)

            let path: [JSONSubscriptType] = ["user","id"]
            let name = json[path].string
            print("AAAAA")
            print(name)
        }
}

I can read the first part of user but the second one with id is always nil. 我可以读取用户的第一部分,但具有ID的第二部分始终为nil。 this is the response json: 这是响应json:

{
  "responseCode": 0,
  "responseDescription": "OK",
  "user": "{"id":"MAIL",
        "nickname":"MYNAME",
        "level":"U",
        "status":"A",
        "sex":null,
        "ageGroup":null,
        "address":null,
        "latitude":null,
        "longitude":null,
        "creation_timestamp":"2017-05-10 18:40:21",
        "notification":"1",
        "last_login":"2017-05-11 18:32:07",
        "mobilePreference":null,
        "sport":null,
        "spot":null,
        "token":"LONGTOKENID"}"
}

Thank you vadian, i solved the question with your indication, if anyone have the same problem you can solve like this: //Initialize the first json: 谢谢你,我用你的指示解决了这个问题,如果有人有同样的问题,你可以这样解决://初始化第一个json:

let json = JSON(responseObject)

//Extract the second Json to String //将第二个Json提取为String

let path: [JSONSubscriptType] = ["user"]
let name = json[path].string

//Initialize the second json from string //从字符串初始化第二个json

if let dataFromString = name?.data(using: .utf8, allowLossyConversion: false) 
let jsonuser = JSON(data: dataFromString)

//Access to the data //访问数据

Thank you all, Have nice day. 谢谢大家,祝你有美好的一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM