Trying to read JSON file in Objective-C -


i trying read in json file in objective-c application, unfortunately getting runtimeexception. exact error is:

*** terminating app due uncaught exception 'nsinvalidargumentexception', reason: 'data parameter nil' 

my json file 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 reads in file looks this:

int main(int argc, const char * argv[]) {      @autoreleasepool {         // create managed object context         nsmanagedobjectcontext *context = managedobjectcontext();          // custom code here...         // save 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; } 

what don't understand why nsdata object coming null, when reading in json file? can see i'm doing wrong? record, have tried checking extraneous spaces in json file, , tried placing json file in different places within application folder, , nothing has worked.

thanks in advance reply.

from source code , comments assume application built "command line tool". in case "target membership" checkbox disabled custom resource files json file.

as workaround:

  • go "build phases" of target.
  • click on "add build phase" , select "add copy files".
  • in new build phase, click on + , select json file.

for command line tool, not seem matter "destination" choose. tested "destination: resources". json file copied in same directory contains executable file. now

[[nsbundle mainbundle] pathforresource:@"transactions" oftype:@"json"]; 

should return path , can read file application.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -