SBJsonParser JSONValue 失败。错误是:令牌的非法开始

发布于 2024-12-05 12:01:28 字数 972 浏览 2 评论 0原文

我正在尝试从 iGoogle 计算器获取汇率。我已经成功运行 NSURLConnection 并通过以下方式在 NSData 中构建了结果:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Add the data to our complete response
    [urlResponse appendData:data];
}

我现在正在解析 google 返回的 JSON:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *dataString =[[NSString alloc]initWithData:urlResponse encoding:NSUTF8StringEncoding];
    // log out the result
    NSLog(@" Result %@", dataString );
    NSDictionary *dic = [dataString JSONValue];
    NSLog(@" Dic %@", dic );

我正在使用 NSString 上的 SBJSON 类别来解析 JSON。我的日志输出如下:

URL: http://www.google.com/ig/calculator?hl=en&q=1USD=?CRC
Result {lhs: "1 U.S. dollar",rhs: "501.756147 Costa Rican colones",error: "",icc: true}
-JSONValue failed. Error is: Illegal start of token [l]

我根本看不出 JSON 字符串有什么问题。与此相关的其他答案都没有反映我遇到的问题。

I am trying to get an exchange rate from the iGoogle Calculator. I have successfully run a NSURLConnection and built up the result in an NSData via:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Add the data to our complete response
    [urlResponse appendData:data];
}

I am now parsing the JSON returned by google in:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *dataString =[[NSString alloc]initWithData:urlResponse encoding:NSUTF8StringEncoding];
    // log out the result
    NSLog(@" Result %@", dataString );
    NSDictionary *dic = [dataString JSONValue];
    NSLog(@" Dic %@", dic );

I am using the SBJSON category on NSString to to parse the JSON. My log output is below:

URL: http://www.google.com/ig/calculator?hl=en&q=1USD=?CRC
Result {lhs: "1 U.S. dollar",rhs: "501.756147 Costa Rican colones",error: "",icc: true}
-JSONValue failed. Error is: Illegal start of token [l]

I simply cannot see what is wrong with the JSON string. None of the other answers around this reflect the problem I am having.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半世晨晓 2024-12-12 12:01:28

这不是有效的 JSON 字符串,因为所有字符串都必须位于双引号内。例如,

lhs

应该是

"lhs"

相反。这同样适用于 rhserroricc

与往常一样,http://jsonlint.com 是检查 JSON 字符串是否有效的有用资源。

That’s not a valid JSON string because all strings must be inside double quotation marks. For example,

lhs

should be

"lhs"

instead. The same applies to rhs, error and icc.

As usual, http://jsonlint.com is a useful resource for checking whether a JSON string is valid or not.

怎会甘心 2024-12-12 12:01:28

我同意巴伐利亚的观点。
我在使用 SBJSON 时遇到了同样的错误。

如果是:

{"lhs": "1 U.S. dollar","rhs": "501.756147 Costa Rican colones","error": "","icc": "true"}

你不会有问题,但由于该 json 是由 google 生成的,你必须用双引号将每个键和值括起来。

这不是您需要的全部内容,但您可以参考以下代码:

//assuming its just a simple json and you already stripped it with { and } 
NSString* json = @"asd:\"hello\",dsa:\"yeah\",sda:\"kumusta\"";
//explodes json
NSArray* jsonChunks = [json componentsSeparatedByString:@","];

NSMutableString *trueJson = [[NSMutableString alloc] init];

for (int idx =0; idx < [jsonChunks count]; idx++) {
    //explodes each jsonChunks
    NSArray *chunky = [[jsonChunks objectAtIndex:idx] componentsSeparatedByString:@":"];
    //reconstruction
    if (idx+1 == [jsonChunks count]) {
        [trueJson appendFormat:@"%@:%@",[NSString stringWithFormat:@"\"%@\"",[chunky objectAtIndex:0]],[chunky objectAtIndex:1]];
    }
    else {
        [trueJson appendFormat:@"%@:%@,",[NSString stringWithFormat:@"\"%@\"",[chunky objectAtIndex:0]],[chunky objectAtIndex:1]];
    }
}
NSLog(@"trueJson: %@",trueJson);
//do the realeases yourself Xp

I agree with Bavarious.
I had this same error using SBJSON.

if it was:

{"lhs": "1 U.S. dollar","rhs": "501.756147 Costa Rican colones","error": "","icc": "true"}

You'll have no problem but since that json is generated by google you'll have to enclose each key and values with double quotes.

It's not the whole thing you need but you can refer to this code:

//assuming its just a simple json and you already stripped it with { and } 
NSString* json = @"asd:\"hello\",dsa:\"yeah\",sda:\"kumusta\"";
//explodes json
NSArray* jsonChunks = [json componentsSeparatedByString:@","];

NSMutableString *trueJson = [[NSMutableString alloc] init];

for (int idx =0; idx < [jsonChunks count]; idx++) {
    //explodes each jsonChunks
    NSArray *chunky = [[jsonChunks objectAtIndex:idx] componentsSeparatedByString:@":"];
    //reconstruction
    if (idx+1 == [jsonChunks count]) {
        [trueJson appendFormat:@"%@:%@",[NSString stringWithFormat:@"\"%@\"",[chunky objectAtIndex:0]],[chunky objectAtIndex:1]];
    }
    else {
        [trueJson appendFormat:@"%@:%@,",[NSString stringWithFormat:@"\"%@\"",[chunky objectAtIndex:0]],[chunky objectAtIndex:1]];
    }
}
NSLog(@"trueJson: %@",trueJson);
//do the realeases yourself Xp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文