4

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

3
  • yes just use a local file URL Commented Sep 4, 2013 at 11:02
  • Why on Earth would you want to do that? Commented 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. Commented Mar 18, 2015 at 1:51

4 Answers 4

19

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

Thanks.Let me try that out!
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.

Comments

0

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.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.