Is it possible to parse data from a local json file ,using AFNetworking instead of SBJSON?
-
yes just use a local file URLMax MacLeod– Max MacLeod2013-09-04 11:02:30 +00:00Commented Sep 4, 2013 at 11:02
-
Why on Earth would you want to do that?Cyrille– Cyrille2013-09-04 11:06:18 +00:00Commented Sep 4, 2013 at 11:06
-
2@Cyrille im developing a code where the web service is not done, so for developments sake im simulating the json file received from the network in a local json file but i want to implement the correct code with AFNetworking now.newton_guima– newton_guima2015-03-18 01:51:08 +00:00Commented Mar 18, 2015 at 1:51
4 Answers
You don't need to use AFNetworking for local JSON. You won't have any network related advantage in local files.
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.
Comments
Why not just parse it using the built in NSJSONSerialization classes? That's all AFNetworking is doing behind the scenes. Save yourself some trouble.
1 Comment
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.)