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

时间:2011-05-20 00:39:11

标签: objective-c json plist

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

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

7 个答案:

答案 0 :(得分:147)

如果您使用的是Mac,则可以在命令行中使用plutil工具(这与我认为的开发人员工具一起提供):

plutil -convert json Data.plist

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

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

答案 1 :(得分: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);
}

它做了一些合理的错误检查,但它不是防弹。使用风险自负。

您需要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

答案 2 :(得分:2)

代码执行起来非常简单:

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

答案 3 :(得分:2)

我在python中编写了一个工具来执行此操作。见这里:

http://sourceforge.net/projects/plist2json

在os x或linux发行版的命令行中运行,批量转换目录。它既简短又简单,所以应该很容易根据自己的目的进行修改。

答案 4 :(得分:2)

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

答案 5 :(得分:1)

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

答案 6 :(得分:0)

使用mac utils

转换plist-> json

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

转换json-> plist

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