ios - Store JSON output in NSArray -



ios - Store JSON output in NSArray -

good morning,

i'm trying develop first app , i'm using tableview in order show entries mysql database , i'm having problem when have parse json output.

i have created connection json, need save id in single array, user in array, etc, etc. can see below in sec code, need store them in nsarray. how can that?

that's json

[{"id":"15","user":"1","imagen":"http:\/\/farmaventas.es\/images\/farmaventaslogo.png","date":"2014-09-13"} ,{"id":"16","user":"2","imagen":"http:\/\/revistavpc.es\/images\/vpclogo.png","date":"2014-11-11"}]

and that's tableviewcontroller.m

- (void)viewdidload { [super viewdidload]; nsstring * urlstring = [nsstring stringwithformat:@"http://website.com/service.php"]; nsurl * url = [nsurl urlwithstring:urlstring]; nsdata * info = [nsdata datawithcontentsofurl:url]; nserror * error; nsmutablearray *json = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:&error]; // here have store "user" self.carmakes = [[nsarray alloc] initwithobjects: nil]; // here have store "imagen" self.carmodels = [[nsarray alloc] initwithobjects: nil]; }

thanks in advance.

first of all, want fetch json on thread, dont block main thread it:

@interface viewcontroller () @end @implementation viewcontroller -(void)viewdidload { [super viewdidload]; [self fetchjson]; } -(void)fetchjson { dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_default, 0); dispatch_async(queue, ^{ nsstring * urlstring = [nsstring stringwithformat:@"http://website.com/service.php"]; nsurl * url = [nsurl urlwithstring:urlstring]; nsdata * info = [nsdata datawithcontentsofurl:url]; nserror * error; nsmutablearray *json = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:&error]; // advice create carmodels mutable array , initialise here,before start working json self.carmodels = [[nsmutablearray alloc] init]; @try { nserror *error; nsmutablearray* json = [nsjsonserialization jsonobjectwithdata:thedata options:nsjsonreadingmutablecontainers|nsjsonreadingmutableleaves error:&error]; if (error){ nslog(@"%@",[error localizeddescription]); } else{ for(int i=0;i<json.count;i++){ nsdictionary * jsonobject = [json objectatindex:i]; nsstring* imagen = [jsonobject objectforkey:@"imagen"]; [carmodel addobject:imagen]; } dispatch_async(dispatch_get_main_queue(), ^{ // if want after you're done loading json, here } });

}

ios objective-c json cocoa cocoa-touch

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -