Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parse JSON from a file in iOS? [closed]

I am trying to parse data from a JSON file. I am trying to put that parsed/fetched data into a UIView with a label or in a webview. The JSON file looks something like the following:

{"bodytext": "<p>\n Some lines here about some webpage (&ldquo; <em>Site</>&rdquo;) some more lines here. \n </p>\n\n <p>\n some more stuff here </p> } 

There are posts here on Stack Overflow showing how to parse JSON retrieved from a Web URL, but I actually already have a JSON file I want to parse. How do I parse JSON from a file?

like image 426
Chris Avatar asked Aug 29 '13 21:08

Chris


1 Answers

  1. Create empty text file (New File/Other/Empty) e.g. "example.json"

  2. Paste json string into the file.

  3. Use these lines to get the data:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"json"]; NSData *data = [NSData dataWithContentsOfFile:filePath]; NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 
like image 198
ArturOlszak Avatar answered Sep 20 '22 20:09

ArturOlszak