如何在ios设备中存储来自url的json数据并使用本地数据

时间:2021-12-21 16:53:13

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

我想在我的应用程序中使用json。我可以使用以下代码从url解析json:

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

但我想,当我没有互联网时,我可以使用json。我不知道如何离线使用json(或我的设备本地)

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

你能指导我在这个问题上如何离线使用json吗?

1 个解决方案

#1


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

对于在线和离线json数据,解析方式应该相同,两者都是相同的,只需要在文档目录中使用Unparsed数据说data.json来编写文件,如果你想使用相同的进程来使用json,但是如果你想要使用已解析的json,然后将其写为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];
}

#1


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

对于在线和离线json数据,解析方式应该相同,两者都是相同的,只需要在文档目录中使用Unparsed数据说data.json来编写文件,如果你想使用相同的进程来使用json,但是如果你想要使用已解析的json,然后将其写为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];
}