问题描述
我正在尝试使用ios应用程序中的alamofire从服务器检索字符串,但是问题是响应正在更改,因为它为每个实体都添加了 \字符。这是我的代码:
I am trying to retrieve a string from server using alamofire in an ios app but the problem is that the response is changing as it is prefixing ever entity with "\" character. here is my code:
func getInformation()
{
self.activityIndicator.startAnimating()
let parameters: Parameters = ["SchoolCode": "TCenYrhWUQH7kKLVZ1FQgQ==", "FacultyInfoCode":"cFl9ivLKKKk5PgH4tbi/Gg=="]
Alamofire.request("http://epunjabschool.gov.in/webservice/staffwebservice.asmx/StaffDetails", method: .post, parameters: parameters).responseString{ response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
self.activityIndicator.stopAnimating()
if let JsON = response.result.value {
var string = JsON
if let idx = string.lastIndex(of:"[") {
var index1=idx+1
var index2 = string.lastIndex(of:"]")!-1
var substring = string.substring(from:index1,to:index2)
if let dataFromString = substring.data(using: .utf8, allowLossyConversion: false) {
let json = JSON(data: dataFromString)
// for i in 0..<json.count
// {
var js=json[0]
if(self.defaults.string(forKey: "status") == "hr")
{
self.district.text=js["Name"].stringValue
self.school.text=js["UserMaster_DisplayName"].stringValue
}
else
{
self.district.text=js["Faculty_Name"].stringValue
self.school.text="dfjdnfj"
}
// }
}
}
}
}
}
这是运行相同服务时得到的响应来自浏览器:
here is the response which i am getting when running the same service from broswer:
<string xmlns="http://tempuri.org/">
[{"FacultyInfo_Code":"107406","Faculty_Name":"Vipul bansal","Father_Name":"Vijay kumar","DOB":"21-Sep-1986","Gender":"Male","CasteCategory_Name":"GENERAL","Religion_Name":"HINDU","MaritalStatus_Name":"UnMarried","Disability":"No Disability","Qualification_Acad":"+2","Qualification_Prof":"MSC(IT)","ComputerTypingKnowledge_Name":"Both","Cur_Address":"sikkhan wala road, channy street, house no.2, kotkapura, FARIDKOT, Punjab","Per_Address":"sikkhan wala road, channy street, house no.2, kotkapura, S.A.S. NAGAR, Punjab","Disatance":"27.00","MobileNo":"8427010890","EmailID":"[email protected]"}]~[{"FacultyInfo_Code":"107406","Faculty_Name":"Vipul bansal","Father_Name":"Vijay kumar","DOB":"21-Sep-1986","Gender":"Male","ServiceType":"Regular","StaffType":"Teaching","AppointmentUnder":"PICTES","Current_DesignationName":"Computer Faculty","Subject_Taught":"COMPUTER SCIENCE","DateOfJoining":"18-Sep-2009","Joining_Present_Desination":"29-Oct-2010","DISTRICT_CODE":"5","DISTRICT_NAME":"FARIDKOT","School_Name":"GHS DHURKOT","EDU_BLOCK_NAME":"FARIDKOT-03","udise_code":"03130103102","RecordType":"S","Mobile_No":"8427010890","Email_Id":"[email protected]"}]
</string>
以下是alamofire的回复:
here is the reponse from alamofire:
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<string xmlns=\"http://tempuri.org/\">[{\"FacultyInfo_Code\":\"107406\",\"Faculty_Name\":\"Vipul bansal\",\"Father_Name\":\"Vijay kumar\",\"DOB\":\"21-Sep-1986\",\"Gender\":\"Male\",\"CasteCategory_Name\":\"GENERAL\",\"Religion_Name\":\"HINDU\",\"MaritalStatus_Name\":\"UnMarried\",\"Disability\":\"No Disability\",\"Qualification_Acad\":\"+2\",\"Qualification_Prof\":\"MSC(IT)\",\"ComputerTypingKnowledge_Name\":\"Both\",\"Cur_Address\":\"sikkhan wala road, channy street, house no.2, kotkapura, FARIDKOT, Punjab\",\"Per_Address\":\"sikkhan wala road, channy street, house no.2, kotkapura, S.A.S. NAGAR, Punjab\",\"Disatance\":\"27.00\",\"MobileNo\":\"8427010890\",\"EmailID\":\"[email protected]\"}]~[{\"FacultyInfo_Code\":\"107406\",\"Faculty_Name\":\"Vipul bansal\",\"Father_Name\":\"Vijay kumar\",\"DOB\":\"21-Sep-1986\",\"Gender\":\"Male\",\"ServiceType\":\"Regular\",\"StaffType\":\"Teaching\",\"AppointmentUnder\":\"PICTES\",\"Current_DesignationName\":\"Computer Faculty\",\"Subject_Taught\":\"COMPUTER SCIENCE\",\"DateOfJoining\":\"18-Sep-2009\",\"Joining_Present_Desination\":\"29-Oct-"...
可以看出,问题是alamofire响应中的 \多余字符使我无法解析json数据。为什么alamofire响应中的数据发生变化?
As it can be seen, the problem is the "\" extra character in the alamofire response making it impossible for me to parse the json data. Why is the data changing in alamofire response?
推荐答案
Alamofire不会更改数据,这只是控制台将其打印出来的方式。处理此问题的最简单方法是将ObjectMapper与Alamofire解析此响应。或者,您可以使用以下命令:
Alamofire is not changing the data, this is just the way the console prints it out. The easiest way to handle this would be to simply couple ObjectMapper along with Alamofire to parse this response. OR you could use the following :
func getInformation()
{
self.activityIndicator.startAnimating()
let parameters: Parameters = ["SchoolCode": "TCenYrhWUQH7kKLVZ1FQgQ==", "FacultyInfoCode":"cFl9ivLKKKk5PgH4tbi/Gg=="]
Alamofire.request("http://epunjabschool.gov.in/webservice/staffwebservice.asmx/StaffDetails", method: .post, parameters: parameters, encoding: .JSON).responseJSON{ response in
switch response.result {
case .success:
let json = response.result.value
case .failure(let error):
print(error)
}
}
这篇关于当我在iOS应用中使用alamofire时,为什么服务器的字符串响应发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!