为什么从服务器的string响应改变,当我在ios应用程序中使用alamofire?

我试图检索服务器使用alamofire在一个IOS应用程序的string,但问题是,响应正在改变,因为它是前缀实体与“\”字符。 这里是我的代码:

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" } // } } } } } } 

这里是我从巴斯夫运行相同的服务时得到的回应:

 <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, SAS NAGAR, Punjab","Disatance":"27.00","MobileNo":"8427010890","EmailID":"vipulbansal59@yahoo.in"}]~[{"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":"vipulbansal59@yahoo.in"}] </string> 

这里是来自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, SAS NAGAR, Punjab\",\"Disatance\":\"27.00\",\"MobileNo\":\"8427010890\",\"EmailID\":\"vipulbansal59@yahoo.in\"}]~[{\"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响应中的“\”额外字符,使我无法parsingjson数据。 为什么alamofire响应中的数据变化?

Alamofire不会改变数据,这只是控制台打印出来的方式。 处理这个问题的最简单方法是简单地将ObjectMapper和Alamofire结合起来来parsing这个响应。 或者你可以使用以下内容:

 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) } } 

这是JSON格式的String

 {\"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, SAS NAGAR, Punjab\",\"Disatance\":\"27.00\",\"MobileNo\":\"8427010890\",\"EmailID\":\"vipulbansal59@yahoo.in\"}]~[{\"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\".... 

如果你将parsing它,那么你将能够看到它没有\“

使用NSJSONSerializationparsing它:

 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() //UPdated Code do { let responseObject = try NSJSONSerialization.JSONObjectWithData(response.data, options: []) as! [String:AnyObject] //Use responseObject as NSdictonary to get your data } catch let error as NSError { print("error: \(error.localizedDescription)") } 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" } // } } } } } }