Question

i am new to JSON Parsing. I am using SBJson 3.1 and trying to fetch data in response to which I got above error. I have searched alot on Google, but no one has ever asked or explained what actually error Illegal Start of Token means. So please can somebody explain me this? And how can I debug this error?

The code I have written so far is:

-(IBAction) Start:(id)sender
{
responseData = [[NSMutableData data] retain];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
}

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

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
label.text = [NSString stringWithFormat:@"Connection failed: %@", [error description]];

[connection release];
responseData = nil;
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];

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

[responseData release];

NSDictionary *dict = [responseString JSONValue];

}
Was it helpful?

Solution

First of all instead of:

#define URL @"http://xyz...."

and using it as,

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]];

I just copied it to:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:http://xyz....]];

Secondly, the problem was that json string returned through web service was not correct. When I printed response data, it was something like this:

<3c68746d 6c3e0a20 2020203c 68656164 3e0a2020 20203c2f 68656164 3e0a2020 20203c62 6f64793e 0a5b7b22 55736572 4964223a 2231222c 22557365 724e616d 65223a22 4d616861 20497a68.....>

This link helped me alot to understand what does "Illegal Start of Token" mean.

On web service, following is written:

<html><head></head><body>[
{
    "UserId": "1", .....

Error raised was:

Parse error on line 1: <html><head></head>< ^ Expecting '{', '['

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top