iOS开发之JSON转PLIST(把存储json格式的文件转换成plist文件)

有时开发过程中,经常需要调试接口,但是可能经常没有网络,导致调试无法正常进行。

 

对此可以自己手动设置一些假数据,也可以通过计算机来为我们保存一份真实的网络数据,并自己转化成plist数据,存在本地使用。

 

 

 

## 直接在Mac上运行

```objc

 

NSString *path = @"/Users/xiaoyou/Desktop/lot.json";  

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

[array writeToFile:@"/Users/xiaoyou/Desktop/lot.plist"atomically:YES]; 

   

   

```

 

## 直接在模拟器上运行

 

 

```objc

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

  

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

  

NSString *newPath = [NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle] bundlePath],@"/lot.plist" ];  

  

[array writeToFile:newPath atomically:YES];  

 

```

 

 

踏实的学点东西,让自己丰富,也让生活丰富和充实。

 

 
 
posted @ 2018-01-05 11:35  星星太阳鱼虾蟹  阅读(513)  评论(0编辑  收藏  举报