使用 iPhone 的 JSONKit 解码 JSON bool 时我缺少什么
我有一个 REST 端点,它根据服务器操作是否成功返回一个 BOOL 值。它将响应正文简单地返回为 true
或 false
当我尝试从服务器解码该值时,出现以下错误:
Unexpected token, wanted '{', '}', '[', ']', ',', ':', 'true', 'false', 'null', '"STRING"', 'NUMBER'.
我已经删除了服务器响应方程。现在我只是想让这段代码正常工作:
NSString *result = @"true";
NSNumber *response = [ result objectFromJSONStringWithParseOptions:JKParseOptionNone error:&err ];
if( response == NULL )
NSLog(@"error: %@", err.localizedDescription );
else
NSLog( @"%d", ( int )response );
无论如何,我仍然遇到错误情况并打印出错误消息:
Unexpected token, wanted '{', '}', '[', ']', ',', ':', 'true', 'false', 'null', '"STRING"', 'NUMBER'.
我错过了什么?
I have a REST endpoint that returns a BOOL value depending on whether or not the server operation was successful. It returns the response body as simply true
or false
When I try to decode the value from the server, I get the following error:
Unexpected token, wanted '{', '}', '[', ']', ',', ':', 'true', 'false', 'null', '"STRING"', 'NUMBER'.
I have gone so far as to remove the server response the equation. Now I am just trying to get this code to work:
NSString *result = @"true";
NSNumber *response = [ result objectFromJSONStringWithParseOptions:JKParseOptionNone error:&err ];
if( response == NULL )
NSLog(@"error: %@", err.localizedDescription );
else
NSLog( @"%d", ( int )response );
No matter what, I still run into my error condition and print out the error message:
Unexpected token, wanted '{', '}', '[', ']', ',', ':', 'true', 'false', 'null', '"STRING"', 'NUMBER'.
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将
result
设置为NSString
@"true"
,它不是 JSON。相反,您应该从服务器获取
NSString *response
,然后对其应用objectFromJSONStringWithParseOptions:error:
。要检查有效的 JSON,请尝试 http://jsonlint.com/。
You set your
result
to theNSString
@"true"
, which is not JSON.Rather you should get a
NSString *response
from the server and then applyobjectFromJSONStringWithParseOptions:error:
to it.To check valid JSON, try http://jsonlint.com/.
"true"
不是有效的 json 字符串"true"
isnt a valid json string