嗨,大家需要帮助,我该如何用迅速的alamofire解析此响应

时间:2019-12-11 06:00:29

标签: swift parsing alamofire jsonresponse

我想解析此JSON响应任何帮助。

{
  "id": 7,
  "chef": 1,
  "user": {
        "user_id": 1,
        "username": "test",
        "user_email": "test@test.tn"
  },
  "post": {
       "id": 24,
       "lieu": "test",
       "date": "12/05/2018"
  }
}

1 个答案:

答案 0 :(得分:0)

您可以按照以下方式解析json:-

    //start loader
    Alamofire.request(yourURL, method: .get, parameters: nil, encoding: URLEncoding.default, headers: yourHeader).responseJSON {
                 respo in

                 if respo.result.isSuccess {
                    // stop loader
                    let json = respo.result.value! as? [String:Any] ?? [:]
                    let user = json["user"]as? [String:Any] ?? [:] //value of user key
                    let user_id = user["user_id"]as? String ?? "" //value of user_id key
                 } else {
                    // stop loader
                    print("Something went Wrong")
                 }
              }

通过标题而不是yourHeader

我希望它对您有用...:)