字符981周围未转义的控制字符。解析JSON

时间:2016-08-31 11:08:55

标签: ios json 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)
            }
        }

它在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.."
      }
    ]
  }
}

4 个答案:

答案 0 :(得分:7)

根据你的说法,问题与" \ n"

有关

所以我建议你可以添加" \"这对你有用,如下所示

" \ n" = GT; " \\ N'#34;

因为这是特殊字符调用退格符。

希望你得到答案

答案 1 :(得分:0)

NSLog你收到的NSData,看看你在字节981周围找到了什么。未转义的控制字符是它们是不可见的,所以你不能在NSString中看到它们,但你会看到它们在NSData。

如果您的数据长度为981字节或非常接近,则您的代码可能会处理不完整的JSON数据,而这些数据几乎总是会失败;这是需要解决的问题。如果某些项之间存在控制字符(例如两个数组元素之间),那么这可能是服务器代码中的错误。

答案 2 :(得分:0)

我花了一些时间弄清楚49546是什么。在此处添加它可以使其他人更轻松。 如果您的问题是字符49546周围的转义控制字符

用\\ t

替换\ t

答案 3 :(得分:0)

可以肯定的是(当人们制作傻瓜复制/粘贴...时),我将对象安全地构建: ..

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: "")

因此发送Up时没有问题。