IOS Development JSON Plist example detailed _ios

Source: Internet
Author: User

IOS JSON turn plist

Read JSON data from the Xx.json file and write to the Xx.plist file with the following implementation code:

NSString *path = @ "/users/android_ls/desktop/city_province.json"; 
  Nsarray *array = [nsjsonserialization jsonobjectwithdata:[nsdata Datawithcontentsoffile:path] Options: Nsjsonreadingmutableleaves Error:nil]; 
  [Array writetofile:@ "/users/android_ls/desktop/city_province.plist" atomically:yes]; 

Note: The above code fragment must be run on the emulator

If you change the code snippet above to read:

NSString *path = [[NSBundle mainbundle] pathforresource:@ "City_province.json" oftype:nil]; 
MyLog (@ "path =%@", path); 
 
Nsarray *array = [nsjsonserialization jsonobjectwithdata:[nsdata Datawithcontentsoffile:path] Options: Nsjsonreadingmutableleaves Error:nil]; 
 
NSString *newpath = [NSString stringwithformat:@ "%@%@", [[NSBundle Mainbundle] bundlepath],@ "/city_province.plist"]; 
 MyLog (@ "NewPath =%@", NewPath); 
 
[Array Writetofile:newpath atomically:yes]; 

Note: Copy the City_province.json file to the supporting files directory before testing

Put the test on the simulator and print the log as follows:

2014-10-15 22:38:03.224 ywbapp[11578:60b] path =/users/android_ls/library/application Support/iPhone Simulator/7.1/ Applications/0909d47b-a2b6-467d-9e19-396a73383d8a/ywbapp.app/city_province.json
2014-10-15 22:38:03.225 YWBAPP[11578:60B] NewPath =/users/android_ls/library/application Support/iphone simulator/7.1/applications/ 0909d47b-a2b6-467d-9e19-396a73383d8a/ywbapp.app/city_province.plist

Put the test on the real machine and print the log as follows:

2014-10-15 22:40:59.796 ywbapp[3127:60b] path =/var/mobile/applications/4dab17cc-f307-4d1b-b78d-80e9b5b4343f/ Ywbapp.app/city_province.json
2014-10-15 22:40:59.805 ywbapp[3127:60b] NewPath =/var/mobile/applications/ 4dab17cc-f307-4d1b-b78d-80e9b5b4343f/ywbapp.app/city_province.plist

The path is correct, but the file cannot be found in the appropriate directory.

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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.