1

Feeding the json-parser with the this data: http://mapadosradares.com.br/api/get_initial_load yields this error: Token 'start of array' not expected after outer-most array or object

Here is my code:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"Connection didReceiveData of length: %u", data.length);

    // Printing the received data
    size_t length = [data length];
    unsigned char aBuffer[length];
    [data getBytes:aBuffer length:length];
    //aBuffer[length - 1] = 0;
    NSLog(@"\n\n\n\n%s\n\n\n\n", aBuffer);

    SBJsonStreamParserStatus status = [parser parse:data];

    if (status == SBJsonStreamParserError) {
        NSLog(@"Parser error: %@", parser.error);

    } else if (status == SBJsonStreamParserWaitingForData) {
        NSLog(@"Parser waiting for more data");
    }
}

As far as I can tell the JSON is perfectly fine. Any thoughts?

UPDATE:

Here's the parser initalization:

- (void) getInitialLoad
{
    adapter = [[SBJsonStreamParserAdapter alloc] init];
    parser = [[SBJsonStreamParser alloc] init];

    adapter.delegate = self;
    parser.delegate = adapter;

    NSString *url = @"http://mapadosradares.com.br/api/get_initial_load";
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]
                                             cachePolicy:NSURLRequestUseProtocolCachePolicy    timeoutInterval:60.0];
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
Raphael
  • 7,972
  • 14
  • 62
  • 83

1 Answers1

2

Are you properly initializing the parser between requests? You haven't shown your code, but it seems like this would be a reasonable error to expect if you ran two successive calls to the feed through the parser.

By the way, I ran the feed output through the excellent JSON parser at http://jsonlint.com and it does appear to be fine.

Seamus Campbell
  • 17,816
  • 3
  • 52
  • 60
  • I've updated my question with the parser initialization. The other request is handled by a different parser. Can they be sharing state? – Raphael Feb 10 '12 at 01:41
  • Is that the only place where you initialize and fire an NSURLConnection whose delegate is this object? Try modifying your log statement at the beginning of connection:didReceiveData: to print the address of the connection object; if you see more than one connection talking to your parser, that's probably the issue. – Seamus Campbell Feb 10 '12 at 01:47
  • I'll try that but I don't believe that's the cause since the data prints correctly. – Raphael Feb 10 '12 at 01:49
  • Now I'm confused. You were right, there's another connection using that object as a delegate however the object is not initiating any other request! I'll debug a little more, but is it possible for the request to be firing twice on the code I posted? – Raphael Feb 10 '12 at 02:01
  • And now I'm embarrassed. There was indeed a second call that I completely forgot. – Raphael Feb 10 '12 at 02:22