objective c - Update plist with json data in iOS -


i saving json content in plist file.

//fetch json nsstring *filepath = [[nsbundle mainbundle] pathforresource:@"sample" oftype:@"json"]; nsdata *data = [nsdata datawithcontentsoffile:filepath];  //get json in dictionary format nsdictionary *dict = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:nil]; nslog(@"dict: %@",dict);  //get plist path nsstring *errordesc; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory1 = [paths objectatindex:0]; nsstring *plistpath = [documentsdirectory1 stringbyappendingpathcomponent:@"sampledata.plist"]; nslog(@"plistpath : %@",plistpath);  //save data json plist nsstring *error; data = [nspropertylistserialization datafrompropertylist:dict                                                   format:nspropertylistxmlformat_v1_0                                         errordescription:&error]; if(data) {     if ([data writetofile:plistpath atomically:yes]) {         nslog(@"data saved.");     }else {         nslog(@"did not managed save nsdata.");     } } else {     nslog(@"%@",errordesc); } 

and working perfectly.

now suppose, has added new item in json. how append new item plist.

ps: here, dont want rewrite entire plist. want check whether json updated or not. if json updated want update plist appending new item.

use like:

nsmutabledictionary *plistcontent = [[nsmutabledictionary alloc] initwithcontentsoffile:plistpath]; [plistcontent setobject:value forkey:key]; [plistcontent writetofile:plistpath atomically:yes]; 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -