Pay*_*yar 6 json ios swift alamofire
错误域= NSCocoaErrorDomain代码= 3840"字符981周围未转义的控制字符." UserInfo = {NSDebugDescription =字符981周围未转义的控制字符.}
我在响应请求时遇到上述错误.
以下是代码行:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
它在Postman中提供JSON响应.
我在邮递员中得到的回应:
{
"orderdetails": {
"status_code": "200",
"status_message": "Order details",
"billingandshipping": {
"billing": {
"firstname": "first",
"lastname": "last",
"email": "aa@bbb.com",
"address": "dasdesfrew",
"city": "Rajkot",
"area": "University Road",
"pincode": "360003",
"phone": "1234567890",
"mobileno": "1234567891"
},
"shipping": {
"firstname": "first",
"lastname": "last",
"email": "aa@bbb.com",
"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.."
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
Ash*_*iya 10
根据你的说法,有与"\n"有关的问题
所以我建议你可以添加"\",这对你有用,如下所示
"\n"=>"\\n"
因为这是特殊字符调用退格符.
希望你能得到答案
从 Swift 5 开始,您可以使用以下语法将其简单地声明为原始字符串文字,而不是手动添加另一个\有效的JSON 字符串:\n
let jsonString = #"{"comment": "gdhdj\nfghd.g\nghj\n\n\n\n\n\n\n\n\n\n.."}"#
Run Code Online (Sandbox Code Playgroud)
多行也可以工作:
let jsonString = #"""
{
"comment": "gdhdj\nfghd.g\nghj\n\n\n\n\n\n\n\n\n\n.."
}
"""#
Run Code Online (Sandbox Code Playgroud)
请注意,包装 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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5292 次 |
| 最近记录: |