json 和plist使用终端互转

2019-09-25  本文已影响0人  NanNan

1、注意⚠️:NSBundle下的plist文件知有读的权限,没有写的权限。要写对plist文件进行读写,需要在沙河目录下创建。plist文件。

2、使用终端转换

json转plist文件

plutil -convert xml1 h5_json.json -o data.plist

plist转json

plutil -convert json data.plist -o data.json

3、本地plist文件写数据。

//json

    NSString *jsonPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"DemoPresetApps.bundle/h5_json"] ofType:@"json"];

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

    NSMutableDictionary *jsonDic = [[NSMutableDictionary alloc] init];

    [jsonDicsetObject:arrayforKey:@"123"];

    //plist

    NSString *filePatch = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"NAMApplist.plist"];

    NSString *plistPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"DemoPresetApps.bundle/NAMApplist"] ofType:@"plist"];

    NSDictionary *dataDic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

    NSMutableDictionary *dataMutalDic = [[NSMutableDictionary alloc] init];

    [dataMutalDicaddEntriesFromDictionary:dataDic];

    [dataMutalDicaddEntriesFromDictionary:jsonDic];

    BOOLisSave = [jsonDicwriteToFile:filePatchatomically:YES];

    if(isSave) {

        NSLog(@"123");

    }else{

        NSLog(@"456");

    }

上一篇下一篇

猜你喜欢

热点阅读