4

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.

4Gred__
  • 96
  • 1
  • 3
  • 11
  • How did you determine that the data is too large? And how large is too large? – user3386109 Jun 15 '16 at 03:27
  • I invoked the `[NSByteCountFormatter stringFromByteCount:data.length countStyle:NSByteCountFormatterCountStyleFile]);` function to calculate the length in KB. – 4Gred__ Jun 15 '16 at 03:29
  • And what did it tell you? – user3386109 Jun 15 '16 at 03:31
  • when the data is around 200 bytes, I can correctly store it in a NSDictionary; but unfortunately not for data with 21 KB – 4Gred__ Jun 15 '16 at 03:32
  • 1
    `NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding])` Try that and see what you get. If the data is truly JSON, it should be printable. – user3386109 Jun 15 '16 at 03:44

5 Answers5

7

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.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • I believe this is correct, however to actually solve the issue requires an `NSMutableData` instance variable that is appended to in `didReceiveData` and finally parsed as JSON when `didCompleteWithError` is called. – Droppy Jun 15 '16 at 09:23
3

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

Rahul
  • 152
  • 4
2

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);
    }
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
1

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 .

Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
scandy
  • 11
  • 1
1

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 .

NavodaP
  • 218
  • 3
  • 18