繁体   English   中英

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

[英]Command-line tool for converting PLIST to JSON?

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

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

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

plutil -convert json Data.plist

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

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

以下完成了工作——

// 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.h放在与convertPlistToJSON.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

有一种本地方式,可以将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];

使用 mac 工具

将 plist 转换为 json

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

将json转换为plist

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

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

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 个文件。

Filename.plist转换为Filename.json

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

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

我用python写了一个工具来做到这一点。 看这里:

http://sourceforge.net/projects/plist2json

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

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

Python 有内置的 plist 支持,我们可以指定一个自定义的默认函数来指定当序列化失败时要做什么(例如字节)。 如果您不关心字节字段,我们可以使用以下 python 单线将它们序列化为'<not serializable>'

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

这需要标准输入,解析 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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM