【问题标题】:JSONValue failed. Error is: Illegal start of token [<]JSONValue 失败。错误是:令牌的非法开始 [<]
【发布时间】:2012-09-02 21:44:26
【问题描述】:

我是 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];

}

【问题讨论】:

  • 你的 JSON 输入是什么?
  • 我只是提供 URL “xyz/Final/webserviceName.php” 来获取整个数据用于测试目的。
  • 哦,是的,我可以完全用它来告诉你问题出在哪里。告诉我数据是什么怎么样?
  • 数据格式为:[{"UserId":"1","UserName":"Mahnor ahmad","Address":"Block-l Naz, Kh","EmailId":" maha@xyz.com","Pswd":"1234567","Dob":"2012-06-05","Gender":"female","SecAns":"biryani","CountryId":"3" ,"城市":"卡拉奇","ProfessionId":"3","SecQId":"1","zip":"0"}]。还有很多这样的。
  • 你确定这正是函数正在读取的内容吗?如果用输出函数替换 parse 函数,会得到这样的结果吗?

标签: json ios4 runtime-error sbjson


【解决方案1】:

首先代替:

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

并将其用作,

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

我刚刚复制到:

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

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

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

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

在web服务上,写如下:

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

引发的错误是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    相关资源
    最近更新 更多