Search code examples
iphonejsontouchjson

iPhone unable to parse JSON results from YELP


I'm attempting to parse validated JSON from a yelp search result.

This correctly spits out the json as expected (confirmed in simulator browser and my own).

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSString *dump = [[[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding] autorelease];
    
    NSLog(@"Did Recieve data: %@",  dump);
    [JSONData appendData:data];
}

But when my connection finishes loading I'm having a hard time extracting the results and parsing the data:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{   
    NSLog(@"Connection Did Finish Loading");
    
    NSError *error = nil;
    id cureLocations = [[CJSONDeserializer deserializer] deserializeAsDictionary:JSONData error:&error];
    [JSONData release];
        
    NSLog(@"Connection finished loading: %@", error);
}

I get: Connection finished loading: Error Domain=CJSONDeserializerErrorDomain Code=-11 "The operation couldn’t be completed. (CJSONDeserializerErrorDomain error -11.)"

I switched to TouchJSON from SBJSON because I wasn't able to extract it from that framework either. I've attempted loading it into Dictionaries and Arrays with null as the result. At this point I've been banging my head on the keyboard for hours and would greatly appreciate any input.

JSON sample

Update:

I am a dummy. I hadn't initialized JSONData. Please accept my apologies for wasting your time and thanks for your suggestions.


Solution

  • Ugh, after further review of the application it seems that I rushed to copy my samples into this project and forgot to initialize JSONData:

    self.JSONData = [[[NSMutableData alloc] init]autorelease];
    

    Then I updated my didReceiveData method:

        - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        [[self JSONData] appendData:data];
    }
    

    And everything is now working as expected. This is the second time I've run into this error. I guess I always expected the debugger to pick it up. Thanks for everyones time and assistance.