In my project, I need to send a JSON object to a web service API call. I converted JSON from an array.
do { let theJSONData = try NSJSONSerialization.dataWithJSONObject( param , options: NSJSONWritingOptions(rawValue: 0)) var theJSONText : String = String(data: theJSONData, encoding: NSASCIIStringEncoding)! print(theJSONText) theJSONText = theJSONText.stringByReplacingOccurrencesOfString("\\", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil) print(theJSONText) let newParam = ["ESignData":theJSONText] } catch let error as NSError { print(error) }
it correctly prints the line as
{"EntNum":"47","JobNo":"1737753","ClientID":"100","HospNo":"1","QAReason":"","DoctorNo":"1694","Action":"Sign"} {"EntNum":"47","JobNo":"1737753","ClientID":"100","HospNo":"1","QAReason":"","DoctorNo":"1694","Action":"Sign"}
Now when I try to send this newParam dictionary to an API call, it contains "\" in the string parameters of the JSON string.
WebService.PostURL(mainLink, methodname: ESIGNTHISDOC, param: newParam, userName: AUTH_USERNAME, password: AUTH_PWD, CompletionHandler: { (success, response) in })
And in this web service method, I have a print option. A.
Param = { ESignData = "{\"EntNum\":\"47\",\"JobNo\":\"1737753\",\"ClientID\":\"100\",\"HospNo\":\"1\",\"QAReason\":\"\",\"DoctorNo\":\"1694\",\"Action\":\"Sign\"}"; }
Now I know that this is obvious in iOS because of βin line. Now the problem is that the Android application has many APIs and the API developer does not want to update his code in accordance with us.
I know that this problem occurs due to adding a JSON string in the dictionary as a parameter. But I do not have the proper excuse for this, therefore, if any evidence would also be useful for me to convince him.
Any solution for converting a JSON string without a backslash in iOS? I need a fix on my part, if possible. Any help would be appreciated.
EDIT:
On the server side, he needs
ESignData = {"EntNum":"47","JobNo":"1737753","ClientID":"100","HospNo":"1","QAReason":"","DoctorNo":"1694","Action":"Sign"}
If I pass this as a parameter to POSTMAN, this will give a successful message. But not with our object with a "\" in it.
EDIT 2:
Now print the newParam dictionary:
print(newParam) print("-------------------------") print(newParam["ESignData"])
And magazines:
["ESignData": "{\"EntNum\":\"47\",\"JobNo\":\"1737754\",\"ClientID\":\"100\",\"HospNo\":\"1\",\"QAReason\":\"\",\"DoctorNo\":\"1694\",\"Action\":\"Sign\"}"] ------------------------- Optional("{\"EntNum\":\"47\",\"JobNo\":\"1737754\",\"ClientID\":\"100\",\"HospNo\":\"1\",\"QAReason\":\"\",\"DoctorNo\":\"1694\",\"Action\":\"Sign\"}")
And by debugging:
Printing description of newParam: βΏ 1 elements βΏ [0] : 2 elements - .0 : "ESignData" - .1 : "{\"EntNum\":\"47\",\"JobNo\":\"1737754\",\"ClientID\":\"100\",\"HospNo\":\"1\",\"QAReason\":\"\",\"DoctorNo\":\"1694\",\"Action\":\"Sign\"}"
So it shows that it is in our dictionary. All "combined \.