Iphone SBJsonParser JSONValue失败。错误是:令牌的启动非法
我正在尝试从iGoogle计算器中获取汇率。我已成功运行NSURLConnection并通过以下方式在NSData中建立结果:Iphone SBJsonParser JSONValue失败。错误是:令牌的启动非法,iphone,objective-c,ios,sbjson,Iphone,Objective C,Ios,Sbjson,我正在尝试从iGoogle计算器中获取汇率。我已成功运行NSURLConnection并通过以下方式在NSData中建立结果: - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // Add the data to our complete response [urlResponse appendData:data]; } 我现在正在解析google在中返回的JSON
- (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字符串有什么问题。围绕这一点的其他答案都没有反映出我所面临的问题 这不是有效的JSON字符串,因为所有字符串都必须在双引号内。比如说,
lhs
应该是
"lhs"
相反。这同样适用于rhs
、error
和icc
通常,是检查JSON字符串是否有效的有用资源。这不是有效的JSON字符串,因为所有字符串都必须位于双引号内。比如说,
lhs
应该是
"lhs"
相反。这同样适用于rhs
、error
和icc
通常,是检查JSON字符串是否有效的有用资源。我同意。
我在使用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
//假设它只是一个简单的json,并且您已经用{and}将其剥离
NSString*json=@“asd:\“hello\”,dsa:“yeah\”,sda:“kumusta\”;
//分解json
NSArray*jsonChunks=[json组件由字符串分隔:@“,”];
NSMutableString*trueJson=[[NSMutableString alloc]init];
对于(int idx=0;idx<[jsonChunks count];idx++){
//爆炸每个jsonChunks
NSArray*chunky=[[jsonChunks objectAtIndex:idx]组件由字符串@分隔::“];
//重建
如果(idx+1==[jsonChunks计数]){
[trueJson appendFormat:@“%@:%@”,[NSString stringWithFormat:@“\“%@\”,[chunky objectAtIndex:0],[chunky objectAtIndex:1];
}
否则{
[trueJson appendFormat:@“%@:%@,[NSString stringWithFormat:@”\“%@\”,[chunky objectAtIndex:0],[chunky objectAtIndex:1];
}
}
NSLog(@“trueJson:%@”,trueJson);
//做真实的自己Xp
我同意你的观点。
我在使用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
//假设它只是一个简单的json,并且您已经用{and}将其剥离
NSString*json=@“asd:\“hello\”,dsa:“yeah\”,sda:“kumusta\”;
//分解json
NSArray*jsonChunks=[json组件由字符串分隔:@“,”];
NSMutableString*trueJson=[[NSMutableString alloc]init];
对于(int idx=0;idx<[jsonChunks count];idx++){
//爆炸每个jsonChunks
NSArray*chunky=[[jsonChunks objectAtIndex:idx]组件由字符串@分隔::“];
//重建
如果(idx+1==[jsonChunks计数]){
[trueJson appendFormat:@“%@:%@”,[NSString stringWithFormat:@“\“%@\”,[chunky objectAtIndex:0],[chunky objectAtIndex:1];
}
否则{
[trueJson appendFormat:@“%@:%@,[NSString stringWithFormat:@”\“%@\”,[chunky objectAtIndex:0],[chunky objectAtIndex:1];
}
}
NSLog(@“trueJson:%@”,trueJson);
//做真实的自己Xp
天啊,我真是瞎了眼;谢谢已经错过了几个小时了。天啊,我是如此的盲目;谢谢已经错过了几个小时了。