【问题标题】:"JSONValue" Keyword not getting recognised while using SBJson ?使用 SBJson 时无法识别“JSONValue”关键字?
【发布时间】:2013-07-15 19:54:48
【问题描述】:

我已将SBJson 文件夹和#import "SBJson.h" 复制到我的项目中

但我还是没有得到

NSDictionary *result = [strResult JSONValue];

即使是 Xcode 也不显示任何选项 JSONValue;

即使我写了JSONValue,它也会提示我错误

No visible @interface for 'NSString' declares the selector 'JSONValue'

【问题讨论】:

  • 为此我需要 json 框架
  • 我刚刚复制了json文件夹,提示我arc不可用。
  • 看看你下面的第一个答案。使用它。
  • NSJSONSerialization 好用,看下面的答案。

标签: ios objective-c json sbjson


【解决方案1】:

你不需要SBJson

有一个原生类 NSJSONSerialization 可以更快地完成此操作,并且无需导入任何内容。

NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[strResult dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];

更好的方法是直接从他的请求中使用NSData...

NSDictionary *result = [NSJSONSerialization JSONObjectWithData:theJSONDataFromTheRequest options:0 error:nil];

【讨论】:

  • 这就是我们现在应该如何解析 JSON。 SBJson 是为我们构建 JSON 序列化之前的时代而创建的。
  • NSString *response = [NSString stringWithFormat:@"http://www.....php"]; const char *convert = [response UTF8String]; NSString *responseString = [NSString stringWithUTF8String:convert]; NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; NSArray *ads = [responseString JSONValue]; NSLog(@"%@",[[ads objectAtIndex:0] objectAtIndex:1]); 为什么这段代码不起作用@fogmeister
  • @gokcokkececi 你想做什么?您拥有的字符串只是文本而不是 JSON。你在这里没有从网上得到任何东西。
  • strResult 是什么?是 NSData 还是 NSString 还是 NSURL 还是 NSDIctionary
  • @Fogmeister 请建议
【解决方案2】:

如果您仍想使用 SBJson,请将 #import "SBJSON.h" 替换为 #import "JSON.h" 即可。 p>

【讨论】:

  • 作为SBJson的作者,我不明白你为什么会这么说。这意味着您使用的是超过 3 年的版本,而不是使用最近的版本。无论如何,除非您需要支持 iOS4 或更低版本,否则您应该使用 Apple 的 NSJSONSerialisation
猜你喜欢
  • 1970-01-01
  • 2020-10-09
  • 2018-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-21
  • 1970-01-01
相关资源
最近更新 更多