使用AFNetworking从json本地文件解析数据

时间:2022-01-04 09:47:03

Is it possible to parse data from a local json file ,using AFNetworking instead of SBJSON?

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

4 个解决方案

#1


18  

You don't need to use AFNetworking for local JSON. You won't have any network related advantage in local files.

您不需要将AFNetworking用于本地JSON。您将无法在本地文件中获得任何与网络相关的优势。

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];

Now use jsonDictionary as per your need.

现在根据需要使用jsonDictionary。

#2


3  

Why not just parse it using the built in NSJSONSerialization classes? That's all AFNetworking is doing behind the scenes. Save yourself some trouble.

为什么不使用内置的NSJSONSerialization类解析它?这就是AFNetworking在幕后所做的一切。省去一些麻烦。

#3


3  

NSData *theData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:templateFileName ofType:@"json"]];
NSDictionary *parsedData = [NSJSONSerialization JSONObjectWithData:theData options:NSJSONReadingMutableContainers error:nil];

Hope this will help you.

希望这会帮助你。

#4


1  

To load the local JSON file in iOS we need to go to build phases, by selecting the target, load the file using copy bundle resources. Then build the file. Simply adding it by following 'file-> new file -> other -> empty' procedure wont work.(I was struggling for 4 hours for getting the expected result.)

要在iOS中加载本地JSON文件,我们需要通过选择目标来构建阶段,使用复制包资源加载文件。然后构建文件。只需按照'file-> new file - > other - > empty'程序添加它就行了。(我为了得到预期的结果而奋斗了4个小时。)

P.S. I cannot comment as there are less reputation.

附:由于声誉较低,我无法发表评论。

#1


18  

You don't need to use AFNetworking for local JSON. You won't have any network related advantage in local files.

您不需要将AFNetworking用于本地JSON。您将无法在本地文件中获得任何与网络相关的优势。

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];

Now use jsonDictionary as per your need.

现在根据需要使用jsonDictionary。

#2


3  

Why not just parse it using the built in NSJSONSerialization classes? That's all AFNetworking is doing behind the scenes. Save yourself some trouble.

为什么不使用内置的NSJSONSerialization类解析它?这就是AFNetworking在幕后所做的一切。省去一些麻烦。

#3


3  

NSData *theData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:templateFileName ofType:@"json"]];
NSDictionary *parsedData = [NSJSONSerialization JSONObjectWithData:theData options:NSJSONReadingMutableContainers error:nil];

Hope this will help you.

希望这会帮助你。

#4


1  

To load the local JSON file in iOS we need to go to build phases, by selecting the target, load the file using copy bundle resources. Then build the file. Simply adding it by following 'file-> new file -> other -> empty' procedure wont work.(I was struggling for 4 hours for getting the expected result.)

要在iOS中加载本地JSON文件,我们需要通过选择目标来构建阶段,使用复制包资源加载文件。然后构建文件。只需按照'file-> new file - > other - > empty'程序添加它就行了。(我为了得到预期的结果而奋斗了4个小时。)

P.S. I cannot comment as there are less reputation.

附:由于声誉较低,我无法发表评论。