【问题标题】:Command-line tool for converting PLIST to JSON?用于将 PLIST 转换为 JSON 的命令行工具?
【发布时间】:2011-05-20 00:39:11
【问题描述】:

是否有可用于将 .plist 文件转换为 JSON 的命令行工具?

如果不是,在 Mac 上使用 Objective-C 或 C 创建一个的方法是什么?例如,有用于 Objective-C 的 JSONKit。如何打开一个 .plist 文件,将其传递给 JSONKit,然后将其序列化为 JSON?

【问题讨论】:

    标签: objective-c json plist


    【解决方案1】:

    如果你在 Mac 上,你可以在命令行上使用 plutil 工具(我相信这是开发者工具自带的):

    plutil -convert json Data.plist
    

    如 cmets 中所述,这将覆盖现有数据。输出到新文件

    plutil -convert json -o Data.json Data.plist
    

    【讨论】:

    • 这将覆盖原来的 plist。要防止此命令覆盖原始文件,请传递-o 标志。即plutil -convert json Data.plist -o Data.json
    • 一个复杂的问题是某些 Plist 数据类型不能被 plutil 转换。我发现的解决方法是在将 plist 传递给 plutil 之前对其进行一些预处理。具体到我正在使用的 plist,我不得不用 <string> 替换 <data><date> 标签。
    • 您的第二个命令将同样转换原始 plist 文件。应该是plutil -convert json -o Data.json Data.plist。也就是说,原来的plist文件应该是最后了。 @亚当
    • 使用plutil -convert -r json 接收人类可读格式的json
    • 传递一个连字符作为输出文件打印到标准输出。 plutil -convert json -o - file.json
    【解决方案2】:

    下面的工作完成了——

    // convertPlistToJSON.m
    #import <Foundation/Foundation.h>
    #import "JSONKit.h"
    
    int main(int argc, char *argv[]) {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
      if(argc != 3) { fprintf(stderr, "usage: %s FILE_PLIST FILE_JSON\n", argv[0]); exit(5); }
    
      NSString *plistFileNameString = [NSString stringWithUTF8String:argv[1]];
      NSString *jsonFileNameString  = [NSString stringWithUTF8String:argv[2]];
    
      NSError *error = NULL;
    
      NSData *plistFileData = [NSData dataWithContentsOfFile:plistFileNameString options:0UL error:&error];
      if(plistFileData == NULL) {
        NSLog(@"Unable to read plist file.  Error: %@, info: %@", error, [error userInfo]);
        exit(1);
      }
    
      id plist = [NSPropertyListSerialization propertyListWithData:plistFileData options:NSPropertyListImmutable format:NULL error:&error];
      if(plist == NULL) {
        NSLog(@"Unable to deserialize property list.  Error: %@, info: %@", error, [error userInfo]);
        exit(1);
      }
    
      NSData *jsonData = [plist JSONDataWithOptions:JKSerializeOptionPretty error:&error];
      if(jsonData == NULL) {
        NSLog(@"Unable to serialize plist to JSON.  Error: %@, info: %@", error, [error userInfo]);
        exit(1);
      }
    
      if([jsonData writeToFile:jsonFileNameString options:NSDataWritingAtomic error:&error] == NO) {
        NSLog(@"Unable to write JSON to file.  Error: %@, info: %@", error, [error userInfo]);
        exit(1);
      }
    
      [pool release]; pool = NULL;
      return(0);
    }
    

    它会进行一些合理的错误检查,但它不是防弹的。使用风险自负。

    您需要JSONKit 来构建该工具。将JSONKit.mJSONKit.hconvertPlistToJSON.m放在同一目录下,然后编译:

    shell% gcc -o convertPlistToJSON convertPlistToJSON.m JSONKit.m -framework Foundation
    

    用法:

    shell% convertPlistTOJSON
    usage: convertPlistToJSON FILE_PLIST FILE_JSON
    
    shell% convertPlistTOJSON input.plist output.json
    

    读入input.plist,并将打印出来的漂亮JSON写入output.json

    【讨论】:

      【解决方案3】:

      执行此操作的代码相当简单:

      NSArray* array = [[NSArray arrayWithContentsOfFile:[@"~/input.plist" stringByExpandingTildeInPath]]retain];
      SBJsonWriter* writer = [[SBJsonWriter alloc] init];
      NSString* s = [[writer stringWithObject:array] retain];
      [s writeToFile:[@"~/output.json" stringByExpandingTildeInPath] atomically:YES];
      [array release];
      

      我从来没有让它接受参数,因为我只需要做 3 个文件。

      【讨论】:

        【解决方案4】:

        我在 python 中编写了一个工具来执行此操作。见这里:

        http://sourceforge.net/projects/plist2json

        在 os x 或 linux 发行版上从命令行工作,批量转换目录。它简短而简单,因此应该很容易根据您自己的目的进行修改。

        【讨论】:

          【解决方案5】:

          有一种本地方式,将plist 转换为json。它叫做NSJSONSerialization

          这是一个关于如何使用它的示例,并将plist 文件转换为json 文件:

          NSDictionary *plistDict = [NSDictionary dictionaryWithContentsOfFile:@"input.plist"];
          
          NSError *error;
          NSData *jsonData = [NSJSONSerialization dataWithJSONObject:plistDict options:NSJSONWritingPrettyPrinted error:&error];
          NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
          [jsonString writeToFile:@"output.json" atomically:NO encoding:NSUTF8StringEncoding error:&error];
          

          【讨论】:

            【解决方案6】:

            使用 mac 工具

            将 plist 转换为 json

            plutil -convert json -o output.json input.plist
            

            将json转成plist

            plutil -convert xml1 input.json -o output.plist
            

            【讨论】:

              【解决方案7】:

              Filename.plist 转换为Filename.json

              plutil -convert json -r -e json Filename.plist
              

              -convert 表示格式,-r 使输出更易于阅读,-e 指定扩展名

              【讨论】:

                猜你喜欢
                • 2012-10-16
                • 1970-01-01
                • 2012-01-21
                • 2012-11-03
                • 2021-11-03
                • 1970-01-01
                • 2021-03-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多