0

我是 JSON 解析的新手。我正在使用 SBJson 3.1 并尝试获取数据以响应我遇到的上述错误。我在谷歌上搜索了很多,但没有人问过或解释过错误的真正Illegal Start of Token含义。所以请有人可以解释一下吗?我该如何调试这个错误?

到目前为止我写的代码是:

-(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];

}
4

1 回答 1

1

首先,而不是:

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

并将其用作,

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

我刚刚把它复制到:

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

其次,问题是通过Web服务返回的json字符串不正确。当我打印响应数据时,它是这样的:

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

这个链接帮助我理解了“Illegal Start of Token”是什么意思。

在 Web 服务上,编写以下内容:

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

引发的错误是:

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

于 2012-09-02T23:29:32.680 回答