1

I want to use json in my application. I can parse json from url with this code :

#import "ViewController.h"
@implementation ViewController
{
    NSDictionary *titles;
    NSMutableData *Data;
    NSArray *fileContent;
    NSArray *folderContent;
    NSMutableArray *all;
}
@synthesize Table;
- (void)viewDidLoad
{
    [super viewDidLoad];  
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    NSURL *url =[NSURL URLWithString:@"http://192.168.1.100/veis/root/developers/api/filemanager.php?key=5147379269&getlist=root"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc]initWithRequest:request delegate:self];

}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    Data = [[NSMutableData alloc]init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [Data appendData:theData];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    titles = [NSJSONSerialization JSONObjectWithData:Data options:kNilOptions error:nil];
    fileContent = [titles objectForKey:@"fileContent"];
    folderContent = [titles objectForKey:@"folderContent"];
    all = [[NSMutableArray alloc]init];
    [all addObjectsFromArray:folderContent];
    [all addObjectsFromArray:fileContent];
    [Table reloadData];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:@"Error" message:@"The Connection has been LOST" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [errorView show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

but I want when I dont have internet I can to use of json. I dont know how to use of json in way offline (or local in my device)

can you guide me in this issue that how to use json in way offline

janatan
  • 415
  • 2
  • 5
  • 11
  • Just open a local file. What part are you having trouble with? – jscs Apr 22 '13 at 07:12
  • possible duplicate of [iPhone/iOS JSON parsing tutorial](http://stackoverflow.com/questions/5813077/iphone-ios-json-parsing-tutorial) – Srikar Appalaraju Apr 22 '13 at 07:12
  • Josh Caswell my friend my problem is that I dont know how to use json without internet. (to use plist??? or download file json in my device and next to use it??? which way???) – janatan Apr 22 '13 at 07:16

1 Answers1

6

The way parsing should be same for Online and Offline json data, both are same, just you need to write a file using Unparsed data say data.json in Documents Directory, if you want to use the same process to use json, but if you want to use parsed json, then write it as plist..

You can try to use the following methods

-(void)saveJsonWithData:(NSData *)data{

     NSString *jsonPath=[[NSSearchPathForDirectoriesInDomains(NSUserDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingFormat:@"/data.json"];

     [data writeToFile:jsonPath atomically:YES];

}

-(NSData *)getSavedJsonData{
    NSString *jsonPath=[[NSSearchPathForDirectoriesInDomains(NSUserDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingFormat:@"/data.json"];

    return [NSData dataWithContentsOfFile:jsonPath]
}

Then call the function as

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    [self saveJsonWithData:data];
}
iphonic
  • 12,615
  • 7
  • 60
  • 107
  • my friend I understand that you tell me 2 way is to use json offline. first I can to use download json file in my device and then parse json from .json file and second I can parsed json in .plist file then to use .plist???? right??? – janatan Apr 22 '13 at 07:30
  • my friend can you explain me more about download .json file in my device and to use next,please??? – janatan Apr 22 '13 at 07:38
  • It will be in `Documents Directory` of the App – iphonic Apr 22 '13 at 08:14
  • 1
    No my friend I tell that in top code isn't url for download .json file – janatan Apr 22 '13 at 08:18