有效的JSON,但来自AFNetworking / NSJSONSerialization的“Cocoa错误3840” [英] Valid JSON, but 'Cocoa error 3840' from AFNetworking/NSJSONSerialization

查看:536
本文介绍了有效的JSON,但来自AFNetworking / NSJSONSerialization的“Cocoa错误3840”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在黑客了几个小时试图解决这个问题,没有用 - 似乎我的唯一选择是张贴在这里,看看有没有人可以揭示这个问题的一些光。这可能是AFNetworking的一个问题,或(更可能),它可能是我的代码的问题。



我使用的代码完美地为99%的我的应用程序中的操作。我正在开发一个应用程序,通过构建JSON搜索,发送它们并从服务器接收响应来利用Elastic Search。



这是一个返回的JSON示例:

  {
took:4,
timed_out:false,
_shards:{
total:5,
successful:5,
failed:0
},
hits:{
total:1,
max_score:null,
hits:[
{
_index:asx,
_type:61a88d3848b00655d9aa59db70847318,
_id:b91f9257744fedb4ef1c127e275c127c,
_score:null,
_source:{
value:22/06 / 1998
},
sort:[
4.439049394553e-312
]
}
]
}
}

现在,将其插入jsonlint.com(了解一点JSON格式化)看到这是有效的JSON。



我使用AFHTTPClient子类为了POST我的请求和接收数据。这里是我使用到POST的代码:

  [super postPath:path parameters:parameters success:^(AFHTTPRequestOperation * operation, NSDictionary * response){
NSData * responseData = [(AFJSONRequestOperation *)operation responseData];
NSDictionary * responseDictionary;

if(responseData!= nil){
responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:NULL];
}

if(success){
success(operation,responseDictionary);
}
}失败:^(AFHTTPRequestOperation * operation,NSError * error){
NSData * responseData = [(AFJSONRequestOperation *)operation responseData];
NSDictionary * responseDictionary;

if(responseData!= nil){
responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:NULL];
}

if(failure){
failure(operation,error,responseDictionary);
}
}];

这里没有什么特别的,我只是把响应转换成JSON。

$然而,问题是,对于这个特定请求,AFNetworking认为响应是失败的,因此失败块中的代码是唯一正在执行的代码。我收到以下错误:

 (lldb)po错误
$ 1 = 0x0adbc710错误Domain = NSCocoaErrorDomain Code = 3840 操作无法完成(Cocoa错误3840.)(数字在字符279周围为NaN)UserInfo = 0xad968d0 {NSDebugDescription =数字在字符279周围以NaN形式结束}

JSON包含一个指数数字(具体来说是4.439049394553e-312),但这是有效的数字,应该能够被解析。当我尝试使用NSJSONSerialization解析响应数据时,我得到相同的NSError。只是为了澄清 - AFNetworking给了与NSJSONSerialization相同的错误消息。



我找不到任何其他谁是与我有相同的问题,不能找出为什么我的JSON不能被解析。它离开我的应用程序有一个非常大的bug,我不能解决。



如果任何人可以阐明这个问题的一些光,这将是巨大的。如果这不是AFNetworking的问题,如果你能指向我一个有用的资源,也将是真棒。

解决方案

/ div>

NSJSONSerialization 使用 NSDecimalNumber 表示数字,

  [NSDecimalNumber decimalNumberWithString:@4.439049394553e-312] 

已返回 NaN ,因为 NSDecimalNumber 只能代表数字

 尾数x 10 ^指数其中`-128 <=指数<= 127。 

这似乎是 NSJSONSerialization的一个限制



我用SBJsonParser做了一个快速测试,它已经同样的问题。


I've been hacking away for hours trying to solve this problem to no avail - it seems like my only option is to post here to see if anyone can shed some light on this issue. It may be an issue with AFNetworking, or (more likely), it may be an issue with my code.

The code I am using works perfectly for 99% of the operations within my application. I am developing an app that leverages Elastic Search by building JSON searches, sending them off and receiving a response from the server.

Here is an example of the JSON that is returned:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": null,
        "hits": [
            {
                "_index": "asx",
                "_type": "61a88d3848b00655d9aa59db70847318",
                "_id": "b91f9257744fedb4ef1c127e275c127c",
                "_score": null,
                "_source": {
                    "value": "22/06/1998"
                },
                "sort": [
                    4.439049394553e-312
                ]
            }
        ]
    }
}

Now, plugging this into jsonlint.com (and knowing a bit about JSON formatting), it's easy to see that this is valid JSON.

I am using an AFHTTPClient subclass in order to POST my request and receive data. Here is the code I am using to POST:

[super postPath:path parameters:parameters success:^(AFHTTPRequestOperation *operation, NSDictionary *response) {
    NSData *responseData = [(AFJSONRequestOperation *)operation responseData];
    NSDictionary *responseDictionary;

    if (responseData != nil) {
        responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:NULL];
    }

    if (success) {
        success(operation, responseDictionary);
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSData *responseData = [(AFJSONRequestOperation *)operation responseData];
    NSDictionary *responseDictionary;

    if (responseData != nil) {
        responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:NULL];
    }

    if (failure) {
        failure(operation, error, responseDictionary);
    }
}];

Nothing really special here, I'm just turning the response into some JSON.

However, the problem is that for this in particular request, AFNetworking is deeming the response as a failure, so the code in the failure block is the only code being executed. I am getting the following error:

(lldb) po error
$1 = 0x0adbc710 Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Number wound up as NaN around character 279.) UserInfo=0xad968d0 {NSDebugDescription=Number wound up as NaN around character 279.}

The JSON contains an exponential number in it (4.439049394553e-312 specifically), however this is valid number and should be able to be parsed. I get the same NSError when I try to parse the response data using NSJSONSerialization. Just to clarify - AFNetworking is giving me the same error message as NSJSONSerialization does.

I cannot find anyone else who is having the same issue as me, and cannot figure out why my JSON can't be parsed. It's leaving my app with a very large bug which I cannot fix.

If anyone could shed some light on this issue, that'd be great. If it's not an issue with AFNetworking, if you could point me to a helpful resource that would also be awesome. Of course, if you require any more information, please do ask

Thank you.

解决方案

NSJSONSerialization uses NSDecimalNumber to represent numbers, and

[NSDecimalNumber decimalNumberWithString:@"4.439049394553e-312"]

already returns NaN, because NSDecimalNumber can represent only numbers

mantissa x 10^exponent         where `-128 <= exponent <= 127`.

So this seems to be a "restriction" (or bug) of NSJSONSerialization, that it works only with numbers in a certain range.

I made a quick test with "SBJsonParser" and it had the same problem.

这篇关于有效的JSON,但来自AFNetworking / NSJSONSerialization的“Cocoa错误3840”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆