Backslash "\" added to JSON string for web service in swift

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 \.

+5
source share
1 answer

Today I ran into this problem. It seems to me that the default encoding for any NSURLRequest is a string. So, somewhere between my creation of the dictionary query and the parsing of the server, backslashes will appear, and the server has problems with my payload.

I solved the problem by explicitly stating that my payload was JSON by setting the content type header.

  [authRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 

Now when I create JSON data from a dictionary, backslashes are not displayed and the server can parse everything correctly.

Below is a code snippet for completeness:

  NSMutableURLRequest *authRequest = [[[NSURLRequest alloc] initWithURL:authURL] mutableCopy]; [authRequest setHTTPMethod:@"POST"]; NSURLSession *session = [NSURLSession sharedSession]; [authRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSDictionary *bodyDictionary = @{@"User_Name": user, @"Password_Hash": password}; if ([NSJSONSerialization isValidJSONObject:bodyDictionary]) { NSError *error; NSData *bodyData = [NSJSONSerialization dataWithJSONObject:bodyDictionary options:0 error:&error]; if (!error) { [authRequest setHTTPBody:bodyData]; } else { NSLog(@"Unable to convert to JSON DATA %@", error.localizedDescription); } } 
+1
source

All Articles