尝试在Objective-C中读取JSON文件 [英] Trying to read JSON file in Objective-C

查看:120
本文介绍了尝试在Objective-C中读取JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的Objective-C应用程序中读取json文件,但不幸的是我遇到了RuntimeException异常.确切的错误是:

I am trying to read in a json file in my Objective-C application, but unfortunately I am getting a RuntimeException. The exact error is:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'

我的json文件称为"Transactions.json

My json file is called "Transactions.json

[{ "transaction": "300001", "date": "1/1/11", "type": "ABC", "status": "State1" },
  { "transaction": "300002", "date": "2/2/12", "type": "XYZ", "status": "State2" },
  { "transaction": "300003", "date": "3/3/13", "type": "ABC", "status": "State3" },
  { "transaction": "300004", "date": "2/2/12", "type": "XYZ", "status": "State2" },
  { "transaction": "300005", "date": "3/3/13", "type": "ABC", "status": "State3" },
  { "transaction": "300006", "date": "2/2/12", "type": "XYZ", "status": "State2" },
  { "transaction": "300007", "date": "3/3/13", "type": "ABC", "status": "State3" },
  { "transaction": "300008", "date": "2/2/12", "type": "XYZ", "status": "State2" },
  { "transaction": "300009", "date": "3/3/13", "type": "ABC", "status": "State3" },
  { "transaction": "300010", "date": "4/4/14", "type": "XYZ", "status": "State4" } ]

我读取文件的方法如下:

My method that reads in the file looks like this:

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        // Create the managed object context
        NSManagedObjectContext *context = managedObjectContext();

        // Custom code here...
        // Save the managed object context
        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Error while saving %@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
            exit(1);
        }

        NSError *err = nil;
        NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"Transactions" ofType:@"json"];
        NSLog(@"Hello: %@", dataPath);
        NSArray *transaction = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath] options:kNilOptions error:&err];

                NSLog(@"Transaction list: %@", transaction);

    }

    return 0;
}

我不明白的是,当我在读取JSON文件时,为什么NSData对象显示为null?有人可以看到我在做什么吗?仅作记录,我尝试检查JSON文件中的多余空间,并尝试将JSON文件放置在应用程序文件夹中的其他位置,但没有任何效果.

What I don't understand is why is the NSData object coming up as null, when I am reading in the JSON file? Can anyone see what I'm doing wrong? Just for the record, I have tried checking for extraneous spaces in my JSON file, and tried placing the JSON file in different places within my application folder, and nothing has worked.

首先感谢所有回复的人.

Thanks in advance to all who reply.

推荐答案

从您的源代码和注释中,我假定该应用程序是作为 命令行工具".在这种情况下,"Target MemberShip"复选框实际上是 对于自定义资源文件(如JSON文件)禁用.

From your source code and your comments I assume that the application is built as a "Command Line Tool". In that case the "Target MemberShip" checkbox is actually disabled for custom resource files like your JSON file.

一种解决方法:

  • 转到目标的构建阶段".
  • 单击添加构建阶段",然后选择添加复制文件".
  • 在新的构建阶段,单击+并选择您的JSON文件.
  • Go to the "Build Phases" of the target.
  • Click on "Add Build Phase" and select "Add Copy Files".
  • In the new build phase, click on + and select your JSON file.

对于命令行工具,选择哪个目标"似乎无关紧要. 我使用目标:资源"对其进行了测试.将JSON文件复制到同一文件中 包含可执行文件的目录.现在

For a command line tool, it does not seem to matter which "Destination" you choose. I tested it with "Destination: Resources". The JSON file is copied in the same directory that contains the executable file. Now

[[NSBundle mainBundle] pathForResource:@"Transactions" ofType:@"json"];

应返回该路径,您可以从应用程序中读取文件.

should return that path and you can read the file from your application.

这篇关于尝试在Objective-C中读取JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆