Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.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
Iphone 如何从应用程序中本地访问json文件_Iphone_Ios_Objective C_Json_Initwithcontentsoffile - Fatal编程技术网

Iphone 如何从应用程序中本地访问json文件

Iphone 如何从应用程序中本地访问json文件,iphone,ios,objective-c,json,initwithcontentsoffile,Iphone,Ios,Objective C,Json,Initwithcontentsoffile,我试图弄清楚如何从应用程序中本地访问json文件。目前,我一直在从如下服务器远程使用json文件: jsonStringCategory = @"http://****categories?country=us"; } // Download the JSON file NSString *jsonString = [NSString stringWithContentsOfURL:[NSURL URLWithSt

我试图弄清楚如何从应用程序中本地访问json文件。目前,我一直在从如下服务器远程使用json文件:

jsonStringCategory = @"http://****categories?country=us";
    }

    // Download the JSON file
    NSString *jsonString = [NSString
                            stringWithContentsOfURL:[NSURL URLWithString:jsonStringCategory]
                            encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding
                            error:nil];

    NSLog(@"jsonStringCategory is %@", jsonStringCategory);

    NSMutableArray *itemsTMP = [[NSMutableArray alloc] init];

    // Create parser 
    SBJSON *parser = [[SBJSON alloc] init];
    NSDictionary *results = [parser objectWithString:jsonString error:nil];

    itemsTMP = [results objectForKey:@"results"];

    self.arForTable = [itemsTMP copy];

    [self.tableView reloadData];
我试过这个:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"categoriesus" ofType:@"json"];


    jsonStringCategory = [[NSString alloc] initWithContentsOfFile:filePath];

谢谢

你能说得更具体些吗?您尝试访问哪个文件?你已经保存了吗

1/对于您的主要问题:您可以使用文件路径创建字典或数组

[NSDictionary dictionaryWithContentsOfFile:<#(NSString *)#>]
[NSArray arrayWithContentsOfFile:<#(NSString *)#>]
然后在我的1/示例中使用“pathToFile”

3/对于您的互联网接入,我建议您检查AFN网络。最好是异步下载;-)(你的是同步的)


我喜欢使用CoreData。遵循以下步骤:

1-)首先创建一个模型,并添加名为jsonvalue的属性字符串类型

2-)创建此函数以保存json文件:

    -(void)saveJson:(NSString*)d
    {

       NSString * data = [d retain];

       NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription entityForName:@"NameObjectModel" inManagedObjectContext:context]];

    NSError *error = nil;
    NSArray *results = [context executeFetchRequest:request error:&error];
    [request release];
    // error handling code
    if(error){

    }
    else{
        Session* favoritsGrabbed = [results objectAtIndex:0];
        favoritsGrabbed.jsonvalue = data;
    }

    if(![context save:&error]){
        NSLog(@"data saved.");
    }
}
3-)创建一个函数来加载json:

-(void)loadJSONFromFile
{

    //Recover data from core data.

    // Define our table/entity to use
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"NameObjectModel" inManagedObjectContext:context];

    // Setup the fetch request
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];

    // Define how we will sort the records - atributo que sera recuperado
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"jsonvalue" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

    [request setSortDescriptors:sortDescriptors];
    [sortDescriptor release];

    // Fetch the records and handle an error
    NSError *error;
    NSMutableArray *mutableFetchResults = [[NSMutableArray alloc]initWithArray:[context executeFetchRequest:request error:&error]];

    if (!mutableFetchResults) {
        // Handle the error.
        // This is a serious error and should advise the user to restart the application
    }

    if(mutableFetchResults.count == 0)
    {
        NSLog(@"the archive is null");
    }

    else if(mutableFetchResults.count > 0)
    {
        NameObjectModel *entity = [mutableFetchResults objectAtIndex:0];
        //NSLog(@"%@",[[entity jsonvalue] JSONValue]);
        NSDictionary * aux = [[entity jsonvalue] JSONValue];

        if([entity jsonvalue]== nil)
        {
            NSLog(@"json is nil");
            NSLog(@"the archive exists but json is nil");
        }

        else {
            // set current json data cache
            [self setJsonCache:aux]; // add to recovery list
        }

    }
    [mutableFetchResults release];
    [request release];
}

别忘了:NameObjectModel=您的NSManagedObject的名称。

您是如何保存它的?哪条路?你应该能够很容易地做“反转”:我将它复制到项目中,就像你将图像拖放到ios appedit中一样:好的。因此,如果它是一个字典,只需执行以下NSDictionary*测试=[[NSDictionary alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@“NameofFile”类型:@“extension”];只需将name of file.extension替换为good things。如果需要,您可以使用数组将其替换为name of file.extension。我认为您提供的信息太多了。他不是在寻找如何做这么多;-)天啊。我知道他正在从您的web服务o_o保存并加载json
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"json"];
NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"json"];
NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];