首页 > 解决方案 > 如何在swift中使用api公共管理器类删除传递数组的json解析错误作为参数?

问题描述

我正在尝试将项目数组作为参数传递,但我得到了任何人请纠正我的错误。

这些是我的参数和网址:

https://www.furnitureinfashion.net/FIF-APP/app_array_cart.php
let proids = ["100","200","300","400"]
let quantity = ["1","2","3","4"]
    let params:[String:Any] = ["product_id":proids,
                               "qty":quantity]

我收到以下错误:

https://www.furnitureinfashion.net/FIF-APP/app_array_cart.php
["qty": ["1", "2", "3", "4"], "product_id": ["100", "200", "300", "400"]]
JSON could not be serialized because of error:
The data couldn’t be read because it isn’t in the correct format.
Error Optional(Alamofire.AFError.responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))
2019-11-26 15:59:53.612426+0530 Furniture in Fashion[13014:196121] Warning: Attempt to present <UIAlertController: 0x7f97d314e800>  on <Furniture_in_Fashion.WishListVC: 0x7f97d2ebaa20> which is already presenting (null)

请使用以下内容找到我的 API 管理器类。

 func apiDataarrayPostMethod(url:String,parameters:[String:Any] , completion: @escaping (_ data:[String:Any]? , _ error:Error?) -> Void)
    {
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
        let manager = Alamofire.SessionManager.default
        manager.session.configuration.timeoutIntervalForRequest = 45

        manager.request(url, method:.post, parameters: parameters, encoding: JSONEncoding.default, headers: headersintoApi()).responseJSON { (response:DataResponse<Any>) in

            UIApplication.shared.isNetworkActivityIndicatorVisible = false 
            if response.result.isSuccess
            {
                print("Response Data: \(response)")

                if let data = response.result.value as? [String:Any]
                {
                    completion(data , nil)

                }else{
                    Helper.Alertmessage(title: "Alert", message: (response.error?.localizedDescription)!, vc: nil)

                    completion(nil,response.error)
                }
            }
            else
            {
                Helper.Alertmessage(title: "Alert", message: (response.error?.localizedDescription)!, vc: nil)
                completion(nil,response.error)
                print("Error \(String(describing: response.result.error))")
            }

        }
    }

标签: iosarraysjsonswiftalamofire

解决方案


您的数据不是有效的 JSON。使用这个网站,您可以验证它。应该是: {"qty": ["1", "2", "3", "4"],"product_id": ["100", "200", "300", "400"]}

{它应该以&开头和结尾},而不是[& ]。另请参见此处


推荐阅读