使用Alamofire发布和解析响应| Swift 5

使用Alamofire发布和解析响应| Swift 5,swift,alamofire,Swift,Alamofire,我有以下JSON,当paramater占领发布时生成: [ { "group": "GR2923", "number": "0239039", } ] 我需要在Swift中解析此数据,并将group的值分配给变量groupValue,将number的值分配给变量numberValue 我尝试过使用类似的东西,但我不确定如何将我拥有的JSON数组实现到Alamofire中: Alamofire.request(url, method: .get)

我有以下JSON,当paramater
占领
发布时生成:

[
    {
        "group": "GR2923",
        "number": "0239039",
    }
]
我需要在Swift中解析此数据,并将
group
的值分配给变量
groupValue
,将number的值分配给变量
numberValue

我尝试过使用类似的东西,但我不确定如何将我拥有的JSON数组实现到Alamofire中:

Alamofire.request(url, method: .get)
  .responseJSON { response in
      if response.data != nil {
        let json = JSON(data: response.data!)
        let name = json["group"][0][""].string
        if name != nil {
          print(name!)
        }
      }
  }
您可以尝试以下方法:

Alamofire.request(url, method: .get)
  .responseJSON { response in
    if let value = response.value {
        let json = JSON(value).arrayValue
        if let name = json[0]["group"].string {
             print(name!)
         }
     }
}
对于带有参数的post请求,您可以使用

 func getResult(url:String, paramKey:[String], paramValue:[Any], completion: @escaping (Bool, Any?) -> Void) {

        let _headers : HTTPHeaders = ["Content-Type":"application/x-www-form-urlencoded"]
        let params : Parameters = getParams(paramKey: paramKey, paramValue: paramValue)

        guard let url = URL(string: url) else {
            completion(false, nil)
            return
        }

        Alamofire.request(url,
                          method: .post,
                          parameters: params, encoding: URLEncoding.httpBody , headers: _headers)
            .validate()
            .responseJSON { response in

                guard response.result.isSuccess else {
                    completion(false, nil)
                    return
                }

                if let value = response.result.value{
                    let json = JSON(value)

                    if json["status_code"].stringValue == "200" {
                        completion(true, json)
                    } else {
                        completion(false, json)
                    }
                }
        }
    }

    func getParams(paramKey:[String], paramValue:[Any]) -> [String:Any] {

        var dictionary =  [String:Any]()

        dictionary.updateValue(Constants.API_TOKEN, forKey: HTTPParams.PARAM_API_TOKEN)

        for index in 0..<paramKey.count {
            dictionary.updateValue(paramValue[index], forKey: paramKey[index])
        }

        return dictionary
    }

您可以使用它来解析json。首先,您必须检查json值是否为NSDictionary

  Alamofire.request(stringURL, method: .get)
        .responseJSON { response in
            if let value = response.result.value as? NSDictionary {
                //    print(value)

                if let group =   value["group"] as? String {
                //    print(group)

}
}
}

您是否使用SwiftyJSON解析响应?不仅仅是AlamofireI需要发布参数
occulation
,才能首先获得响应。参数是如何用Alamofire编写的?我非常感谢您的帮助,如果您有任何问题,请告诉我?@MarkPalson检查我的最新答案。希望这能对你有所帮助。我有点困惑,params和paraValues中会出现什么?在我的例子中,如果在得到结果之前我需要发布的参数是“职业”:“医生”,那将进入哪个?更新后的答案是“职业”:“医生”例子。
  Alamofire.request(stringURL, method: .get)
        .responseJSON { response in
            if let value = response.result.value as? NSDictionary {
                //    print(value)

                if let group =   value["group"] as? String {
                //    print(group)

}
}
}