Reputation: 4402
Error Domain=NSCocoaErrorDomain Code=3840 "Unescaped control character
around character 981." UserInfo={NSDebugDescription=Unescaped control
character around character 981.}
I am getting the above error in response to a request.
Below are the lines of code:
Alamofire.request(.POST, urlStr, parameters: parameter, encoding: .JSON, headers: nil).validate().responseJSON {
response in switch response.result {
case .Success(let JSON):
completionHandler(JSON as! NSDictionary)
case.Failure(let Error):
print(Error)
}
}
It gives a JSON response in Postman.
The response which is I am getting in Postman:
{
"orderdetails": {
"status_code": "200",
"status_message": "Order details",
"billingandshipping": {
"billing": {
"firstname": "first",
"lastname": "last",
"email": "[email protected]",
"address": "dasdesfrew",
"city": "Rajkot",
"area": "University Road",
"pincode": "360003",
"phone": "1234567890",
"mobileno": "1234567891"
},
"shipping": {
"firstname": "first",
"lastname": "last",
"email": "[email protected]",
"address": "dasdesfrew",
"city": "dasdesfrew",
"area": "dcdc",
"pincode": "360003",
"phone": "1234567890",
"mobileno": "1234567891"
}
},
"orders": [
{
"order_id": "77",
"order_date": "09-08-2016 13:05:29",
"delivery_date": "10-08-2016",
"order_items": [
{
"Sr": "1",
"product_name": "Lemon",
"gujtitle": "લીંબુ ",
"product_code": "000057",
"product_price": "108.00",
"product_qty": "2",
"unit": "1 kg.",
"product_total": "216"
}
],
"final_total": "216.00",
"shipping_cost": "0.00",
"order_total": "216.00",
"discount_type": "null",
"discount_amount": "null",
"coupon_name": "null",
"comment": "gdhdj\nfghd.g\nghj\n\n\n\n\n\n\n\n\n\n.."
}
]
}
}
Upvotes: 15
Views: 12714
Reputation: 2717
Since Swift 5, instead of manually adding another \
to your otherwise valid \n
in a JSON string, you could simply declare it as a raw string literal, using this syntax:
let jsonString = #"{"comment": "gdhdj\nfghd.g\nghj\n\n\n\n\n\n\n\n\n\n.."}"#
Multiline works too:
let jsonString = #"""
{
"comment": "gdhdj\nfghd.g\nghj\n\n\n\n\n\n\n\n\n\n.."
}
"""#
Note that wrapping with #
is critical - while the multiline example would compile fine without it, in runtime it would throw an error in the example below with JSONSerialization:
do {
guard let data = jsonString.data(using: .utf8) else { throw SomeError() }
let obj = try JSONSerialization.jsonObject(with: data)
print("valid!")
} catch {
print(error)
}
Upvotes: 6
Reputation: 11646
To be sure (as people make foul copy/paste...), I build my object safe:
...
private final func fillWith(
id: Int,
name: String?
) {
self.id = id
self.productName = name?.replacingOccurrences(of: "\t", with: "")
self.productName = self.productName?.replacingOccurrences(of: "\n", with: "")
So no problem when sending up.
Upvotes: 1
Reputation: 661
I spent some time to figure out what 49546 was.
If your issue is Unescaped control character around character 49546, replace \t
with \\\t
.
Upvotes: 1
Reputation: 318
As per you were told, there is a problem related to "\n".
So I suggest you can add "" which will work for you, like below:
"\n"=> "\\n"
Because this is a special character called a backspace character.
Upvotes: 20
Reputation: 52538
NSLog the NSData that you received and have a look what you find around byte 981. The thing with unescaped control characters is that they are invisible, so you can't see them in an NSString, but you'll see them in the NSData.
If your data has length 981 bytes or very close then there's a chance that your code processed incomplete JSON data which will almost always fail; that's something you need to fix. If there is a control character between some items (say between two array elements) then this might be a bug in the server code.
Upvotes: 2