The data couldn’t be read because it isn’t in the

2020-04-08 07:54发布

Ok, I am now starting with iOS development and currently playing with NSData using Obj-C. Recently I'm using the URLSession:dataTask:didReceiveData method to get NSData using HTTP POST request. The server will be responding a JSON object containing a JSON array.

Something interesting is that when the response data is too large the NSLog part will print out: "The data couldn't be read because it isn't in the correct format". Below is the function:

-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
    NSError *error;
    // get data from NSData into NSDict
    NSDictionary *searchData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    NSArray *test = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    NSMutableDictionary *mdict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSLog(@"what: %@", mdict);
    NSLog(@"received data length: %@", [NSByteCountFormatter stringFromByteCount:data.length countStyle:NSByteCountFormatterCountStyleFile]);
    if (error) {
        NSLog(@"json error: %@", [error localizedDescription]);
    }
}

Just wondering if anyone knows the potential reason causing this problem ??

[Update]

Well, an alternative to solve this problem might be using NSString for data storage. But would have to parse it by myself. I would prefer using NSDictionary though.

5条回答
疯言疯语
2楼-- · 2020-04-08 08:12

At first point you should check if the response received from the API is a valid JSON, can be check by online validators like jsonlint.com. If this is a vaid json, then you are good to go.

Further more you should evaluate the error object on every step when you get it to see which parsing of data cause this problem like:

    NSError *error;
    // get data from NSData into NSDict
    NSDictionary *searchData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

    if (error) {
        NSLog(@"json error: %@", [error localizedDescription]);
    }

    NSArray *test = [NSKeyedUnarchiver unarchiveObjectWithData:data];


    NSMutableDictionary *mdict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSLog(@"what: %@", mdict);

    NSLog(@"received data length: %@", [NSByteCountFormatter stringFromByteCount:data.length countStyle:NSByteCountFormatterCountStyleFile]);

    if (error) {
        NSLog(@"json error: %@", [error localizedDescription]);
    }

Also, as you said that:

The server will be responding a JSON object containing a JSON array.

Are you sure it is returning NS(Mutable)Dictionary ?? Try printing it like this:

    id response = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

    if (error) {
        NSLog(@"json error: %@", [error localizedDescription]);
    }
    else
    {
        NSLog(@"json data : %@", response);
    }
查看更多
我只想做你的唯一
3楼-- · 2020-04-08 08:17

In my case i was hitting POST Method instead of GET. Solved my problem.

查看更多
仙女界的扛把子
4楼-- · 2020-04-08 08:31

Beginner problem: didReceiveData is called when some of the data has arrived, before the data is complete. Read the documentation of didReceiveData, which explains what you need to do. You are trying to parse incomplete data. That won't work.

And when you are handling JSON data, you should never involve any NSString objects, except possibly to log data.

查看更多
beautiful°
5楼-- · 2020-04-08 08:39

if the data is kind of josn ,you can

NSDictionary *dict= [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];

or

[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]) 

,so that you will be get nsdictionary or nsstring data, if you cannot parse it by yourself ,you can user jsonModel tranlte to model .

查看更多
别忘想泡老子
6楼-- · 2020-04-08 08:39

It may be a problem with your json data I have experienced the same. You have to serialize your json data like this below written code

NSDictionary *dict= [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];

If you are populating json data from data base u have to use Nsarray. Hope this will help you .

查看更多
登录 后发表回答