iOS 服务器json数据转成.json文件再转成.plist文件

Copyright © 2017年ZaneWangWang. All rights reserved.

原文地址

一.网络请求数据转json文件

if ([responseObj[@"httpCode"] integerValue] == 200) {

NSDictionary *jsonDic = responseObj[@"data"];

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDic options:0 error:nil];

NSString *filePath= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

NSString *newPath = [filePath stringByAppendingString:@"/hmkj_c_addressList.json" ];

[jsonData writeToFile:newPath atomically:YES];

//转换成功后,文件位置要输出出来便于查找

NSLog(@"newPath = %@", newPath);

}

二.json文件转.plist文件

1.拿以上输出的文件位置,点击finder,弹出如下图:


iOS 服务器json数据转成.json文件再转成.plist文件_第1张图片

2.继续点击前往,如下图,并在前往文件夹中粘贴出刚刚输出的路径,注意不要.json文件名,因为进入的是文件夹.

iOS 服务器json数据转成.json文件再转成.plist文件_第2张图片

3.将json文件拖入工程中,再写如下代码:

NSString *path = [[NSBundle mainBundle] pathForResource:@"hmkj_c_addressList.json" ofType:nil];

NSArray *array = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:NSJSONReadingMutableLeaves error:nil];

NSString *str= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

NSString *plistPath = [str stringByAppendingString:@"/hmkj_c_addressList.plist" ];

//转换成功后,文件位置要输出出来便于查找

NSLog(@"plistPath = %@", plistPath);

[array writeToFile:plistPath atomically:YES];

4.然后然后按相同的找文件方式即可找到plist文件.

保留所有权,未经允许禁止转载。

你可能感兴趣的:(iOS 服务器json数据转成.json文件再转成.plist文件)