4

是否可以使用 AFNetworking 而不是 SBJSON 从本地 json 文件解析数据?

4

4 回答 4

19

您不需要为本地 JSON 使用 AFNetworking。您在本地文件中不会有任何与网络相关的优势。

NSString *filePath = [[NSBundle mainBundle] pathForResource:jsonFileName ofType:@"json"];
NSData *JSONData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:nil];
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:nil];

现在 jsonDictionary根据您的需要使用。

于 2013-09-04T11:07:53.123 回答
3
NSData *theData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:templateFileName ofType:@"json"]];
NSDictionary *parsedData = [NSJSONSerialization JSONObjectWithData:theData options:NSJSONReadingMutableContainers error:nil];

希望这会帮助你。

于 2013-09-04T11:08:36.427 回答
3

为什么不直接使用内置NSJSONSerialization类解析它呢?这就是 AFNetworking 在幕后所做的一切。给自己省点麻烦。

于 2013-09-04T11:01:11.550 回答
0

要在 iOS 中加载本地 JSON 文件,我们需要进入构建阶段,通过选择目标,使用复制包资源加载文件。然后构建文件。只需按照“文件-> 新文件-> 其他-> 空”程序添加它是行不通的。(我为获得预期结果而奋斗了 4 个小时。)

于 2015-02-08T15:30:55.187 回答