在工作中,經(jīng)常有某些固定的值,這些值通常是不會變的,存數(shù)據(jù)庫----麻煩,存本地,這個可以,不過還要在項目中走一次網(wǎng)絡(luò)請求來獲取數(shù)據(jù),不如生成一個plist方便.下面教大家把JSON數(shù)據(jù)轉(zhuǎn)成plist文件的方法
[NetworkHandler getDataByURLString:@"http://app.crhclub.com/v30/flight.ashx?action=airlineCode" BodyString:nil WithDataBlock:^(id result) {
NSDictionary *jsonDic = result;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDic options:0 error:nil];
NSString *filePath= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *newPath = [filePath stringByAppendingString:@"/MaXiaoshuai.json" ];
[jsonData writeToFile:newPath atomically:YES];
NSLog(@"newPath = %@", newPath);
// 將json數(shù)據(jù)轉(zhuǎn)成json文件
NSArray *array = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:newPath] options:NSJSONReadingMutableLeaves error:nil];
NSString *newPlistPath = [NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle] bundlePath],@"/MaXiaoshuai.plist" ];
[array writeToFile:newPlistPath atomically:YES];
NSLog(@"newPlistPath = %@", newPlistPath);
} WithErrorBlock:^(id result) {
}];
```
command + shift + G 輸入newPlistPath的地址 就可以獲取plist文件,拖到項目玩耍去吧
生命不息,代碼不止!碼農(nóng)一枚,請多點贊
等等!!!等等!!!如果懶得自己寫,下面這個工具送給你,輸入json數(shù)據(jù)直接轉(zhuǎn)plist文件
鏈接: https://pan.baidu.com/s/1i4BNXel 密碼: qghh