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

b5lpy0ml  于 2023-08-08  发布在  其他
关注(0)|答案(8)|浏览(120)

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

2ledvvac

2ledvvac1#

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

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

字符串
如果只想覆盖现有文件,可以省略-o参数:

plutil -convert json Data.plist

yhived7q

yhived7q2#

以下是完成任务的方法-

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

7cwmlq89

7cwmlq893#

如果你遇到“invalid object in plist for destination format”的问题,你可能在plist中有类似字节的数据,plutil不认为这些数据可以序列化为json。
Python有内置的plist支持,我们可以指定一个自定义的默认函数来指定当序列化失败时该怎么做(例如如果你不关心bytes字段,我们可以使用下面的python one-liner将它们序列化为'<not serializable>'
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'

ruarlubt

ruarlubt4#

使用mac utils

将plist转换为json

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

字符串

将json转换为plist

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

rn0zuynd

rn0zuynd5#

代码是相当简单的做到这一点:

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

nnt7mjpx

nnt7mjpx6#

我用python写了一个工具来做这个。请看这里:
http://sourceforge.net/projects/plist2json
在os x或linux发行版上从命令行工作,批量转换一个目录。它简短而简单,因此它应该很容易修改为您自己的目的。

ntjbwcob

ntjbwcob7#

有一种原生的方法,将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];

字符串

cbwuti44

cbwuti448#

Filename.plist转换为Filename.json

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

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

相关问题