Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在ios设备中存储url中的json数据并使用本地数据_Ios_Objective C_Json - Fatal编程技术网

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

如何在ios设备中存储url中的json数据并使用本地数据,ios,objective-c,json,Ios,Objective C,Json,我想在我的应用程序中使用json。我可以使用以下代码从url解析json: #import "ViewController.h" @implementation ViewController { NSDictionary *titles; NSMutableData *Data; NSArray *fileContent; NSArray *folderContent; NSMutableArray *all; } @synthesize Table; -

我想在我的应用程序中使用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;
}
但我想当我没有互联网时,我可以使用json。我不知道如何脱机(或在我的设备中本地)使用json


在本期中,您能否指导我如何在脱机方式下使用json

对于
联机
脱机
json数据,解析的方式应该是相同的,两者都是相同的,您只需使用
未解析的
数据编写一个文件,比如
数据.json
文档目录下
,如果您想使用相同的过程来使用
json
,但是如果您想使用解析的json,那么将其写成
plist

您可以尝试使用以下方法

-(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]
}
然后调用函数作为

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

    [self saveJsonWithData:data];
}

对于
Online
Offline
json数据,解析方法应该相同,只需使用
Unparsed
data-say
data.json
Documents目录
,如果要使用相同的进程使用
json
,但如果要使用解析的json,然后将其写成
plist

您可以尝试使用以下方法

-(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]
}
然后调用函数作为

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

    [self saveJsonWithData:data];
}

只需打开一个本地文件。你有什么问题?可能是Josh Caswell的翻版我的朋友我的问题是没有互联网我不知道如何使用json。(要在我的设备中使用plist???或下载文件json,然后使用它???哪种方式???)只需打开一个本地文件。你有什么问题?可能是Josh Caswell的翻版我的朋友我的问题是没有互联网我不知道如何使用json。(要在我的设备中使用plist???或下载文件json,然后使用它???哪种方式???)我的朋友,我知道你们告诉我2种方式是离线使用json。首先我可以在我的设备中使用下载json文件,然后从.json文件解析json,其次我可以在.plist文件中解析json,然后使用.plist????对吗???我的朋友,你能给我解释更多关于在我的设备上下载.json文件和下一步使用的信息吗???它将在AppNo的
文档目录中,我的朋友,我告诉我,在上面的代码不是下载的url。json文件我的朋友,我知道你告诉我两种方法是离线使用json。首先我可以在我的设备中使用下载json文件,然后从.json文件解析json,其次我可以在.plist文件中解析json,然后使用.plist????是吗???我的朋友,你能给我解释更多关于在我的设备中下载.json文件和下一步使用的信息吗???它将在AppNo的
文档目录中
,我的朋友,我告诉你,上面的代码不是下载.json文件的url