objective c - Method always re-writing json file -


nsurl *url = [nsurl urlwithstring:@"file://localhost/users/admin/desktop/json/vivijson.json"];     nsdictionary *regdict = [[nsdictionary alloc] initwithobjectsandkeys:self.loginstring, @"login",                                                                     self.namestring, @"name",                                                                     self.lastnamestring, @"lastname",                                                                     self.emailstring, @"email",                                                                     self.numberstring, @"number",  nil];     nserror *error;     nsmutablearray *regmutarray = [[nsmutablearray alloc] init];     [regmutarray addobject:regdict];     nsdata *jsonconvregarraydata = [nsjsonserialization datawithjsonobject:regmutarray options:nsjsonwritingprettyprinted error:&error];     nsstring *jsonregstring = [[nsstring alloc] initwithdata:jsonconvregarraydata encoding:nsutf8stringencoding];     [jsonconvregarraydata writetourl:url atomically:yes]; 

this method re-writing json, , start again, need add json.

you should first read exiting json mutable array using jsonobjectwithdata using nsjsonreadingmutablecontainers reading options. add new array element mutable array returned jsonobjectwithdata , convert json using datawithjsonobject

here's code.

   nsurl *url = [nsurl urlwithstring:@"file://localhost/users/shared/vivijson.json"];     nsdictionary *regdict = [[nsdictionary alloc] initwithobjectsandkeys:@"self.loginstring, @"login",                              self.namestring, @"name",                              self.lastnamestring, @"lastname",                              self.emailstring, @"email",                              self.numberstring, @"number",  nil];     nserror *error;     nsmutablearray *regmutarray = [[nsmutablearray alloc] init];     [regmutarray addobject:regdict];      nsdata *data = [nsdata datawithcontentsofurl:url];     nsmutablearray *array = nil;     if (data)         array = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers error:nil];     if (array == nil)     {         array = [[nsmutablearray alloc] init];     }     [array addobjectsfromarray:regmutarray];      nsdata *jsonconvregarraydata = [nsjsonserialization datawithjsonobject:array options:nsjsonwritingprettyprinted error:&error];      nsstring *jsonregstring = [[nsstring alloc] initwithdata:jsonconvregarraydata encoding:nsutf8stringencoding];     [jsonregstring writetourl:url atomically:true encoding:nsutf8stringencoding error:nil]; 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -