IOS开发之JSON转PLIST实例详解
IOSJSON转PLIST
从xx.json文件中读取JSON数据,写入到xx.plist文件中,实现代码如下:
NSString*path=@"/Users/android_ls/Desktop/city_province.json"; NSArray*array=[NSJSONSerializationJSONObjectWithData:[NSDatadataWithContentsOfFile:path]options:NSJSONReadingMutableLeaveserror:nil]; [arraywriteToFile:@"/Users/android_ls/Desktop/city_province.plist"atomically:YES];
注:上面的代码片段必须在模拟器上运行
若将上面的代码片段改为如下:
NSString*path=[[NSBundlemainBundle]pathForResource:@"city_province.json"ofType:nil]; MyLog(@"path=%@",path); NSArray*array=[NSJSONSerializationJSONObjectWithData:[NSDatadataWithContentsOfFile:path]options:NSJSONReadingMutableLeaveserror:nil]; NSString*newPath=[NSStringstringWithFormat:@"%@%@",[[NSBundlemainBundle]bundlePath],@"/city_province.plist"]; MyLog(@"newPath=%@",newPath); [arraywriteToFile:newPathatomically:YES];
注:在测试前已将city_province.json文件copy到SupportingFiles目录下
放在模拟器上测试,打印LOG如下:
2014-10-1522:38:03.224YWBAPP[11578:60b]path=/Users/android_ls/Library/ApplicationSupport/iPhoneSimulator/7.1/Applications/0909D47B-A2B6-467D-9E19-396A73383D8A/YWBAPP.app/city_province.json 2014-10-1522:38:03.225YWBAPP[11578:60b]newPath=/Users/android_ls/Library/ApplicationSupport/iPhoneSimulator/7.1/Applications/0909D47B-A2B6-467D-9E19-396A73383D8A/YWBAPP.app/city_province.plist
放在真机上测试,打印LOG如下:
2014-10-1522:40:59.796YWBAPP[3127:60b]path=/var/mobile/Applications/4DAB17CC-F307-4D1B-B78D-80E9B5B4343F/YWBAPP.app/city_province.json 2014-10-1522:40:59.805YWBAPP[3127:60b]newPath=/var/mobile/Applications/4DAB17CC-F307-4D1B-B78D-80E9B5B4343F/YWBAPP.app/city_province.plist
路径是对的,但是在相应的目录下是找不到文件的。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!