Ios Alamofire.PostAPI错误:代码=3840“;字符1周围的值无效
使用Alamofire for.post api,api在postman中提供数据,但不在我的代码中。在下面的代码中发布,请指导我在这里做错了什么:Ios Alamofire.PostAPI错误:代码=3840“;字符1周围的值无效,ios,json,swift3,alamofire,Ios,Json,Swift3,Alamofire,使用Alamofire for.post api,api在postman中提供数据,但不在我的代码中。在下面的代码中发布,请指导我在这里做错了什么: // API calling method: parameters = [ "Address" : "" as AnyObject, "Name" : "" as AnyObject, "ServiceID" : "" as AnyObject, "Rating" : "" as
// API calling method:
parameters = [
"Address" : "" as AnyObject,
"Name" : "" as AnyObject,
"ServiceID" : "" as AnyObject,
"Rating" : "" as AnyObject,
"Price" : "" as AnyObject
]
let headers: Dictionary = [
"" : ""
]
print(parameters)
ApiServices.requestPOSTURL(strURL, params: parameters, headers: headers, success:{
(JSONResponse) -> Void in
CommonMethodsClass.hideHUD(targetView: self.view)
print(JSONResponse["message"])
let strMsg = JSONResponse["message"].stringValue
if (JSONResponse["status"].intValue == 1)
{
}
else
{
CommonMethodsClass.showAlertMessage(vc: self, titleStr: "Error!", messageStr: strMsg)
}
}) {
(error) -> Void in
print(error)
CommonMethodsClass.hideHUD(targetView: self.view)
}
// Api request method:
class func requestPOSTURL(_ strURL : String, params : [String : AnyObject]?, headers : [String : String]?, success:@escaping (JSON) -> Void, failure:@escaping (Error) -> Void){
Alamofire.request(strURL, method: .post, parameters: params, encoding: JSONEncoding.default).responseJSON { (responseObject) -> Void in
print(responseObject)
if responseObject.result.isSuccess {
let resJson = JSON(responseObject.result.value as Any)
success(resJson)
}
if responseObject.result.isFailure {
let error : Error = responseObject.result.error!
failure(error)
}
}
}
错误:失败:
responseSerializationFailed(Alamofire.AFError.ResponseSerializationFaileureReason.jsonSerializationFailed(错误
Domain=nscocaerorrordomain Code=3840“字符周围的值无效
1.“UserInfo={NSDebugDescription=字符1周围的无效值。}”)
更新:对解析的响应,也可能需要更改
{
"status": true,
"message": "",
"data": [
{
"SalonID": "1",
"SalonName": "Affinity",
"SalonEmail": "vay.chaan@th-rce.com",
"SalonPhone": "9999888877",
"SalonMobile": "9999888877",
"SalonAddress": "C-28, Sec-58, India",
"Latitude": "18.5806",
"Longitude": "27.36273",
"Image": null,
"SalonImage": "",
"TimeIntervalminutes": 20,
"AverageRating": 4,
"IsActive": 1
},
{
"SalonID": "3",
"SalonName": "Looks",
"SalonEmail": "rad@th-rce.com",
"SalonPhone": "99998828877",
"SalonMobile": "99998388877",
"SalonAddress": "GP Mall,India",
"Latitude": "",
"Longitude": "",
"Image": null,
"SalonImage": "",
"TimeIntervalminutes": 30,
"AverageRating": 5,
"IsActive": 1
}
]
}
尝试
responseString
而不是responseJSON
,它会起作用
Alamofire.request("URL").responseString { response in
print(response)
if let json = response.result.value {
print("JSON: \(json)")
}
}
尝试
responseString
而不是responseJSON
,它会起作用
Alamofire.request("URL").responseString { response in
print(response)
if let json = response.result.value {
print("JSON: \(json)")
}
}
将
responseJSON
替换为responseString
您需要将字符串转换为值。你能更新你问题中的字符串吗
您可以使用此函数将响应字符串转换为JSON:
func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
字符串:let str=“{\“name\”:\“James\”}”
用法:
let dict=convertToDictionary(text:str)
将responseJSON
替换为responseString
您需要将字符串转换为值。你能更新你问题中的字符串吗
您可以使用此函数将响应字符串转换为JSON:
func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
字符串:let str=“{\“name\”:\“James\”}”
用法:
let dict=convertToDictionary(text:str)
将responseJSON更改为responseString,它将工作:)
将responseJSON更改为responseString,它将起作用:)
将
responseJSON
更改为responseString
,但相同的代码适用于另一个API。我认为此API响应与该API不同。这就是为什么它不能得到。Alamofire发现接收类型存在一些可疑值,这就是它给出此错误的原因。请尝试responseString而不是responseJSON,它将正常工作是的,工作异常。感谢SchangeresponseJSON
至responseString
但同一代码适用于另一个API我认为此API响应与该API不同。这就是为什么它不能得到。Alamofire发现接收类型存在一些可疑值,这就是它给出此错误的原因。请尝试responseString而不是responseJSON,它将正常工作是的,工作异常。谢谢,请引导解析?请引导解析?请检查问题,现在请检查问题,现在