为啥当我在 ios 应用程序中使用 alamofire 时来自服务器的字符串响应会发生变化?
Posted
技术标签:
【中文标题】为啥当我在 ios 应用程序中使用 alamofire 时来自服务器的字符串响应会发生变化?【英文标题】:why is string response from server changing when I am using alamofire in ios app?为什么当我在 ios 应用程序中使用 alamofire 时来自服务器的字符串响应会发生变化? 【发布时间】:2016-12-28 04:41:44 【问题描述】:我正在尝试在 ios 应用程序中使用 alamofire 从服务器检索字符串,但问题是响应正在发生变化,因为它在实体前面加上“\”字符。这是我的代码:
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, S.A.S. 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, S.A.S. 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 响应中的“\”额外字符使我无法解析 json 数据。为什么alamofire响应中的数据会发生变化?
【问题讨论】:
它不是解析数据的正确方法。您必须使用 xml 解析器然后将数据转换为 json。如果您在 json 中获取数组,它将失败。 @rajvir Singh:您共享的响应实际上不是 json 响应,您共享的是示例响应@API 页面。您能否分享您使用代码从服务器获得的确切响应。然后我会清除你的疑虑。 \"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\":\"NoDisability\",\"Qualification_Acad\":\"+2\",\"Qualification_Prof\":\"MSC(IT)\" ,\"ComputerTypingKnowledge_Name\":\"Both\",\"Cur_Address\":\"sikkhan wala road, channy street, house no.2, kotkapura, FARIDKOT, Pkotkapura, S.A.S. NAGAR, Punjab\",\"Disatance\ ":\"27.00\",\"MobileNo\":\"\",\"EmailID\":\"vipulbansal59@yahoo.in\"] 【参考方案1】:Alamofire 不会更改数据,这只是控制台打印出来的方式。处理这个问题的最简单方法是简单地将 ObjectMapper 与 Alamofire 耦合以解析此响应。或者您可以使用以下内容:
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)
【讨论】:
我想检索字符串数据并传递两个参数。请告诉我该怎么做。你的代码没有改变任何东西。我用的是一样的兄弟。 谢谢杰森提图斯【参考方案2】:这是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, S.A.S. 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\"....
如果你会解析它,那么你将能够在没有 \" 的情况下看到它
使用NSJSONSerialization
解析它:
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"
//
【讨论】:
我需要将其用作字符串,然后从中检索 json。我试图插入你的状态,但它要求我使用;分隔多个语句。我试过这个 string = string.replacingOccurrences(of: "\\", with: "")。但它不起作用。我所需要的就是消除所有出现的 \ 字符。请告诉我该怎么做? 刚刚更新的答案。我之前错误地给过你客观的 C 代码。 我认为你的逻辑很复杂。据我了解,您只是从服务器获取 JSON 响应,并希望从某些键中获取数据。所以我提供给你的是解析 JSON 字符串的代码,它会返回一个字典,你可以从中获取数据。 它没有给我直接的 json 格式的字符串。我需要先解析字符串。请告诉我如何从字符串中删除此 \ 字符 请告诉我如何从响应中删除 \ 字符。我试过这个 string = string.replacingOccurrences(of: "\\", with: "") 但它不工作以上是关于为啥当我在 ios 应用程序中使用 alamofire 时来自服务器的字符串响应会发生变化?的主要内容,如果未能解决你的问题,请参考以下文章
为啥当我使用 nativescript 从 iOS 发送表情符号字符时会收到陌生字符串