1

I have the following code that is trying to parse a JSON that I am returning from a website:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

     [connection release];

     NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

     NSLog(@"my ns string = %@", responseString);

     [responseData release];

     NSError *error; 
     NSDictionary *dictionary = [parser objectWithString:responseString   
                                               error:&error]; 
     while (error) { 
         NSLog(@"%@", error); 
         error = [[error userInfo] objectForKey:NSUnderlyingErrorKey]; 
     } 

     NSLog(@"%@", dictionary);


 }

I get these errors:

2011-08-01 20:16:47.273 myJSONParser[1040:b603] Connection didReceiveResponse: 
<NSHTTPURLResponse: 0x4e810f0> - application/json
2011-08-01 20:16:47.279 myJSONParser[1040:b603] Connection didReceiveData of length: 117
2011-08-01 20:16:47.359 myJSONParser[1040:b603] my ns string = 
2011-08-01 20:16:47.361 myJSONParser[1040:b603] Error Domain=org.brautaset.SBJsonParser.ErrorDomain Code=0 "Unexpected end of input" UserInfo=0x4eada30 {NSLocalizedDescription=Unexpected end of input}
2011-08-01 20:16:47.363 myJSONParser[1040:b603] (null)

I do have control over the JSON that I am trying to acquire and inside the PHP that generates the JSON I set the header like this:

header("Content-Type:application/json");
...
echo json_encode($arr);

"Unexpected end of input" leads me to believe that the JSON is somehow malformed, but JSONLint tells me its perfectly valid. What am I doing wrong? Thank you for taking the time to read through this, any advice would really be appreciated!

UPDATE:

I set the responseData like this:

NSMutableData *responseData;

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

         [responseData appendData:data];

     }

And the parser like this (in .h):

SBJsonParser *parser;

And .m:

parser = [[SBJsonParser alloc] init];

Charles Findings:

HTTP/1.1 200 OK
Date: Tue, 02 Aug 2011 03:30:14 GMT
Server: Apache
X-Powered-By: PHP/5.2.15
Transfer-Encoding: chunked
Content-Type: application/json

[{"key1":"Name","key2":"First","key3":"2011-08-13"},{"key1":"Second","key2":"test2","key3":"2011-08-13"}]

Description of the NSData received:

Printing description of data: {length = 117, capacity = 256, bytes = 0x5b7b22686f6d65223a22426c61636b62 ... 30382d3133227d5d}

Doug Molineux
  • 12,283
  • 25
  • 92
  • 144
  • Where is responseData getting initialized? What is the code at that point? From the log output the string is empty. – ThomasW Aug 02 '11 at 02:49

1 Answers1

1

From the code, it looks like we only get "unexpected end of input" when the EOF token is found.

SBJsonParser.m:

case SBJsonStreamParserWaitingForData:
            self.error = @"Unexpected end of input";
            break;

SBJsonStreamParserState.m:

- (SBJsonStreamParserStatus)parserShouldReturn:(SBJsonStreamParser*)parser {
    return SBJsonStreamParserWaitingForData;
}

SBJsonStreamParser.m:

    switch (tok) {
        case sbjson_token_eof:
            return [state parserShouldReturn:self];
            break;
    ... }

Your logs seem to indicate that something is wrong with the NSString. I would examine the response data to make sure it's correct--you can do this with a proxy like Fiddler or Charles, the xcode debugger, or just plain old NSLog().

Edit:

Ok I converted the data you got from the debugger to UTF-8 and this is what I got:

[{"home":"Blackb ... 08-13"}]

You only pasted a partial bit of the byte array, so there should be more where the ellipsis are. Does the 'home' or 'blackb' mean anything to you? It wan't anywhere in the response data that Charles intercepted. Do you do a setLength:0 on responseData in the didReceiveReponse delegate method?

pepsi
  • 6,785
  • 6
  • 42
  • 74
  • Thank you for the response, pepsi, why does NSLog(@"Connection didReceiveData of length: %u", data.length); prints a length 117? The "End of File" Token would indicate the response is empty, or that it wasn't complete, correct? – Doug Molineux Aug 02 '11 at 03:06
  • @Pete: I can't tell where the eof was found; but looking at your logs, something does look wrong with that NSString. Are you sure the encoding is correct? It might help to set up Fiddler/Charles and make sure the data you're receiving is what you expect – pepsi Aug 02 '11 at 03:13
  • I appreciate the suggestion. I updated the question with the findings from Charles – Doug Molineux Aug 02 '11 at 03:33
  • @Pete: The incoming data looks valid to me.. We've narrowed it down to the NSURLConnection delegate methods now. Can you examine the NSData byte array in the debugger and see if it still looks right? – pepsi Aug 02 '11 at 03:40
  • I'm not quite sure what it is that I'm looking for in the debugger, I Printed the description of the object to the console and pasted that into my question. I see that the size is 117, and charles says it's 1.17kb. Not sure if this is a coincidence, one thing is for sure, I haven't seen any evidence that I am receiving the JSON in the first place though – Doug Molineux Aug 02 '11 at 03:53
  • Hey Pepsi, I have basically scrapped this project and remade it, I'm having no issues, I'm not sure what was wrong with this project, and it's a little frustrating I never found out. But I'll have to move on! Thank you for your help on this – Doug Molineux Aug 03 '11 at 17:11
  • @Pete: Aw man, I hate not root causing things like this =p. Glad you got around it though – pepsi Aug 03 '11 at 17:24