欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS开发之JSON转PLIST实例详解

程序员文章站 2023-12-22 12:08:52
 ios json转plist   从xx.json文件中读取json数据,写入到xx.plist文件中,实现代码如下: nsstrin...

 ios json转plist

  从xx.json文件中读取json数据,写入到xx.plist文件中,实现代码如下:

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]; 

     注:上面的代码片段必须在模拟器上运行

    若将上面的代码片段改为如下:

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]; 

      注:在测试前已将city_province.json文件copy到supporting files目录下

     放在模拟器上测试,打印log如下:

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

      放在真机上测试,打印log如下:

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

 路径是对的,但是在相应的目录下是找不到文件的。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: