JSONValue失败了。错误是:非法启动令牌[<]

时间:2012-09-02 21:44:27

标签: json ios4 runtime-error sbjson

我是JSON Parsing的新手。我正在使用SBJson 3.1并尝试获取数据以响应我上面的错误。我在Google上搜索了很多,但没有人曾经问过或解释过实际上错误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];

}

1 个答案:

答案 0 :(得分: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.....>

This链接帮助我了解“非法启动令牌”的含义。

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

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

引发的错误是:

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