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

发布于 2024-11-08 17:50:58 字数 160 浏览 0 评论 0 原文

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

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

Is there a command line tool available for converting .plist files to JSON?

If not, what would be the approach for creating one using Objective-C or C on a Mac? For instance, there is JSONKit, for Objective-C. How would one go about opening a .plist file, pass it to JSONKit, and serialize that as JSON?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

琉璃梦幻 2024-11-15 17:50:58

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

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

如果您只想覆盖现有文件,您可以省略 -o范围:

plutil -convert json Data.plist

If you are on a Mac you can use the plutil tool on the command line (this comes with the developer tools I believe):

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

If you want to just overwrite the existing file, you can omit the -o parameter:

plutil -convert json Data.plist
凶凌 2024-11-15 17:50:58

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

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

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

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

ioreg -rw0 -c AppleSmartBattery -a | python -c '导入 plistlib,sys,json; print(json.dumps(plistlib.loads(sys.stdin.read().encode("utf-8")), default=lambda o:"<不可序列化>"))'

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

If you run into issues with "invalid object in plist for destination format", you probably have bytes-like data in the plist, which plutil does not consider serializable to json.

Python has built-in plist support, and we can specify a custom default function to specify what to do when serialization fails (e.g. on bytes). If you don't care about the bytes fields, we can just serialize them as '<not serializable>' with the following python one-liner:

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

This takes the stdin, parses the plist, then dumps it to json. For example, to get current power information for your Mac, you can run:

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>"))'

and if you wanted to just get the current charging wattage, you could pipe that into jq '.[0].AdapterDetails.Watts'

蓬勃野心 2024-11-15 17:50:58

下面的代码就完成了工作——

// 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

The following gets the job done—

// 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);
}

It does some reasonable error checking, but it's not bullet proof. Use at your own risk.

You'll need JSONKit to build the tool. Place JSONKit.m and JSONKit.h in the same directory as convertPlistToJSON.m, and then compile with:

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

Usage:

shell% convertPlistTOJSON
usage: convertPlistToJSON FILE_PLIST FILE_JSON

shell% convertPlistTOJSON input.plist output.json

Reads in input.plist, and writes the pretty printed JSON to output.json.

是你 2024-11-15 17:50:58

使用 mac utils

将 plist 转换为 json

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

将 json 转换为 plist

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

Using mac utils

Convert plist to json

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

Convert json to plist

plutil -convert xml1 input.json -o output.plist
薄荷港 2024-11-15 17:50:58

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

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

The code is fairly simple to do this:

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];

I never got around to making it accept arguments as I only needed to do 3 files.

少女情怀诗 2024-11-15 17:50:58

我用 python 写了一个工具来做到这一点。请参阅此处:

http://sourceforge.net/projects/plist2json

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

I wrote a tool in python to do this. See here:

http://sourceforge.net/projects/plist2json

Works from command line on os x or linux distros, batch converts a directory. It's short and simple so it should be easy to modify for your own purposes.

謌踐踏愛綪 2024-11-15 17:50:58

有一种本机方法可以将 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];

There is a native way, to convert plist's to json. It's called NSJSONSerialization.

Here is an example on how to use it, and convert a plist file to a json file:

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];
夜还是长夜 2024-11-15 17:50:58

Filename.plist 转换为 Filename.json

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

-convert 表示格式,-r 使输出更加人性化 -可读,-e指定扩展名

Converts Filename.plist to Filename.json:

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

-convert indicates format, -r makes the output more human-readable, -e specifies an extension

温暖的光 2024-11-15 17:50:58

我对将二进制 plist 转换为 json 的复杂程度感到恼火,因此我创建了一个仅适用于浏览器的 Web 工具,用于将二进制 plist 文件转换为 json。

您的文件不会发送到任何地方,一切都在浏览器中完成!

https://gregsadetsky.github.io/i-just -want-to-see-the-plist/

它的代码是开放的,可以找到此处

I was annoyed at how kinda complicated converting binary plists to json was, so I created a web, browser-only tool that converts binary plist files to json.

your files are not sent anywhere, it's all done in the browser!

https://gregsadetsky.github.io/i-just-want-to-see-the-plist/

its code is open and can be found here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文