SBJsonParser JSONValue 失败。错误是:令牌的非法开始
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是有效的 JSON 字符串,因为所有字符串都必须位于双引号内。例如,
应该是
相反。这同样适用于
rhs
、error
和icc
。与往常一样,http://jsonlint.com 是检查 JSON 字符串是否有效的有用资源。
That’s not a valid JSON string because all strings must be inside double quotation marks. For example,
should be
instead. The same applies to
rhs
,error
andicc
.As usual, http://jsonlint.com is a useful resource for checking whether a JSON string is valid or not.
我同意巴伐利亚的观点。
我在使用 SBJSON 时遇到了同样的错误。
如果是:
你不会有问题,但由于该 json 是由 google 生成的,你必须用双引号将每个键和值括起来。
这不是您需要的全部内容,但您可以参考以下代码:
I agree with Bavarious.
I had this same error using SBJSON.
if it was:
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: