用于将PLIST转换为JSON的命令行工具?

dan*_*ean 58 json objective-c plist

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

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

Dyl*_*lan 153

如果你在Mac上,你可以在命令行上使用plutil工具(这附带我认为的开发人员工具):

plutil -convert json Data.plist
Run Code Online (Sandbox Code Playgroud)

如评论中所述,这将覆盖现有数据.输出到新文件

plutil -convert json -o Data.json Data.plist
Run Code Online (Sandbox Code Playgroud)

  • 这将覆盖原始plist.要防止此命令覆盖原始文件,请传递`-o`标志.即`plutil -convert json Data.plist -o Data.json` (14认同)
  • 与此相关的一个复杂因素是plutil无法转换某些Plist数据类型.我找到的解决方法是在将plist传递给plutil之前对plist进行一些预处理.具体到我正在使用的plist,我不得不用`<string>`替换`<data>`和`<date>`标签. (2认同)
  • 请注意-r选项将使其具有人类可读性. (2认同)
  • 使用 `plutil -convert -r json` 以人类可读格式接收 json (2认同)
  • 传递连字符作为输出文件以打印到标准输出。`plutil -转换 json -o - file.json` (2认同)

joh*_*hne 5

以下工作完成 -

// 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);
}
Run Code Online (Sandbox Code Playgroud)

它做了一些合理的错误检查,但它不是防弹.使用风险由您自己承担.

你需要JSONKit来构建工具.放置JSONKit.mJSONKit.h在同一目录中convertPlistToJSON.m,然后使用以下命令编译:

shell% gcc -o convertPlistToJSON convertPlistToJSON.m JSONKit.m -framework Foundation
Run Code Online (Sandbox Code Playgroud)

用法:

shell% convertPlistTOJSON
usage: convertPlistToJSON FILE_PLIST FILE_JSON

shell% convertPlistTOJSON input.plist output.json
Run Code Online (Sandbox Code Playgroud)

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


Chr*_*oba 5

如果您遇到“plist 中目标格式的无效对象”问题,则 plist 中可能有类似字节的数据,plutil 不认为该数据可序列化为 json。

Python有内置的plist支持,我们可以指定一个自定义的默认函数来指定序列化失败时要做什么(例如在字节上)。如果你不关心字节字段,我们可以像'<not serializable>'下面的 python 单行代码一样序列化它们:

python -c 'import plistlib,sys,json; print(json.dumps(plistlib.loads(sys.stdin.read().encode("utf-8")), default=lambda o:"<not serializable>"))'

这需要 stdin,解析 plist,然后将其转储为 json。例如,要获取 Mac 的当前电源信息,您可以运行:

ioreg -rw0 -c AppleSmartBattery -a | python -c 'import plistlib,sys,json; print(json.dumps(plistlib.loads(sys.stdin.read().encode("utf-8")), default=lambda o:"<not serializable>"))'

如果您只想获取当前的充电功率,您可以将其输入jq '.[0].AdapterDetails.Watts'