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

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

如果不是,在Mac上使用Objective-C或C创build一个方法是什么? 例如,对于Objective-C,有JSONKit。 如何打开.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来构build工具。 将JSONKit.mJSONKit.h JSONKit.mJSONKit.h相同的目录中,然后使用下面的代码进行编译:

 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

代码很简单:

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

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

http://sourceforge.net/projects/plist2json

从OS X或Linux发行版的命令行工作,批量转换目录。 它简短,所以应该很容易修改为自己的目的。

有一个本地的方式,把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];