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

 

```

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM