JSON字符0调试

时间:2016-02-21 12:19:47

标签: json swift

如何正确调试错误消息

Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}

我在不同情况下看到这种情况,我总是很难找到真正的原因。 在上面的例子中,我有以下url我从以下地址返回JSON响应:

http://SERVERIP/v1/aeds&latitude=48.1848308444472&longitude=11.2527083605633

通过邮差的服务器响应

{
  "data": [
    {
      "deviceReference": "D200",
      "owner": "Owner 200",
      "latitude": "48.185240",
      "longitude": "11.258890",
      "street": "StreetName",
      "featureTypeId": "1"
    },
    {
      "deviceReference": "D300",
      "owner": "Owner 300",
      "latitude": "48.179290",
      "longitude": "11.253350",
      "street": "StreetName",
      "featureTypeId": "1"
    },
    {
      "deviceReference": "D100",
      "owner": "Owner 100",
      "latitude": "48.176226",
      "longitude": "11.238890",
      "street": "StreetName",
      "featureTypeId": "1"
    }
  ],
  "meta": {
    "Heading 1": "H1",
    "Heading 2": "H2"
  }
}

在我看来,json正确地返回,当我通过Alamofire调用URL并处理它时仍然收到消息。

func getAEDInRange(latitude: Double, longitude: Double, completionHandler: (Result<[AED], NSError>) -> Void) {
    Alamofire.request(Router.GetAEDInRange("?latitude=\(latitude)&longitude=\(longitude)")).responseArray { (response: Response<[AED], NSError>) -> Void in
        print(response.result)
    }
}

responseArray方法

public func responseArray<T: ResponseJSONObjectSerializable>(completionHandler: Response<[T], NSError> -> Void) -> Self {
    let serializer = ResponseSerializer<[T], NSError> { request, response, data, error in

        guard error == nil else {
            return .Failure(error!)
        }

        guard let responseData = data else {
            let failureReason = "Array could not be serialized because input data was nil."
            let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason)
            return .Failure(error)
        }

        let JSONResponseSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
        let result = JSONResponseSerializer.serializeResponse(request, response, responseData, error)

        switch result {

        case .Success(let value):
            let json = SwiftyJSON.JSON(value)

            // Get Data content of JSON
            let jsonData = json["data"]

            var objects: [T] = []
            for (_, item) in jsonData {
                if let object = T(json: item) {
                    objects.append(object)
                }
            }
            return .Success(objects)

        case .Failure(let error):
            return .Failure(error)
        }
    }
    return response(responseSerializer: serializer, completionHandler: completionHandler)
}

浏览器中的文字网址返回:

{"data":[{"deviceReference":"D200","owner":"Owner 200","latitude":"48.185240","longitude":"11.258890","street":"StreetName","featureTypeId":"1"},{"deviceReference":"D300","owner":"Owner 300","latitude":"48.179290","longitude":"11.253350","street":"StreetName","featureTypeId":"1"},{"deviceReference":"D100","owner":"Owner 100","latitude":"48.176226","longitude":"11.238890","street":"StreetName","featureTypeId":"1"}],"meta":{"Heading 1":"H1","Heading 2":"H2"}}

1 个答案:

答案 0 :(得分:0)

如果您的JSON文档看起来很好,但无法解析(“字符0周围的错误”),原因有两个明显的可能性:一,看起来很好的JSON文档不是您的那个正试图解析。二,它看起来很好,但不是很好。例如,如果数据中有零个字节转换为不可见字符。

当错误发生时,将NSData的第一个字节记录为字节并检查其中的确实存在。并在发生错误的确切时间记录转换为字符串的已解析数据

将AllowFragments作为参数传递是无稽之谈。你不期待一个片段。你不会知道如何处理片段。如果有片段,那对你来说是个错误。那么为什么要允许呢?最多会引起混淆(因为现在解析后的响应可以是字典或数组,但也可以是数字,字符串或null)。

BOM(字节顺序标记)实际上是允许在文档的开头,而不是在文档的内部 - 如果文档中间有错误,则需要查找。 “字节0处的错误”通常是零字节。