Writes the JSON string in the TXT file to the plist file

Source: Internet
Author: User

- (void) json2plist{NSString*filepath = [Self applicationdocumentsdirectoryfilename:@"JSON"]; Nsmutablearray*temparray =[[Nsmutablearray alloc] initwithcontentsoffile:filepath]; //The array is empty the first time you add data    if(Temparray.count = =0) {Temparray=[Nsmutablearray array]; }
  //File name (Utf-8 encoding) NSString*path = [[NSBundle mainbundle] Pathforresource:@"City2"OfType:@"txt"]; NSString*contents =[[NSString alloc] Initwithcontentsoffile:path encoding:nsutf8stringencoding Error:nil]; Nsarray*list = [Contents componentsseparatedbystring:@","]; for(NSString *tempstrinchlist) {Nsdictionary*dict =[TempStr Propertylistfromstringsfileformat]; [Temparray addobject:dict]; } BOOL Success=[Temparray Writetofile:filepath Atomically:yes]; NSLog (@"success:%d", success);}#pragmaMark-Gets the path to the plist file in the sandbox-(NSString *) Applicationdocumentsdirectoryfilename: (NSString *) filename{NSString*FilePath; //the document folder in the sandboxNSString *documentdirectory =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; //get the Plist file name that cameNSString *filenamestr = [NSString stringWithFormat:@"%@.plist", FileName]; FilePath=[Documentdirectory Stringbyappendingpathcomponent:filenamestr]; returnFilePath;}
TXT Text Format example:
{id110000 " Beijing ";},{ID120000" Tianjin City ";},{ID130100" Shijiazhuang City ";},{ID 654300 " Altay ";}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.