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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6066350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:51:52  来源:igfitidea点击:

Command-line tool for converting PLIST to JSON?

objective-cjsonplist

提问by dandean

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

是否有可用于将 .plist 文件转换为 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?

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

回答by Dylan

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

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

plutil -convert json Data.plist

as mentioned in the comments, this will overwrite the existing data. To output to a new file

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

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

回答by johne

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 JSONKitto build the tool. Place JSONKit.mand JSONKit.hin the same directory as convertPlistToJSON.m, and then compile with:

您将需要JSONKit来构建该工具。将JSONKit.mJSONKit.h放在与 相同的目录中convertPlistToJSON.m,然后使用以下命令进行编译:

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.

读入input.plist,并将漂亮打印的 JSON 写入output.json.

回答by Rab

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

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

http://sourceforge.net/projects/plist2json

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.

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

回答by skorulis

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.

我从来没有想过让它接受参数,因为我只需要做 3 个文件。

回答by Leandros

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

有一种本地方式,将plist's转换为json. 它被称为NSJSONSerialization

Here is an example on how to use it, and convert a plistfile to a jsonfile:

这是一个关于如何使用它的示例,以及如何将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];

回答by Lal Krishna

Using mac utils

使用 mac 工具

Convert plist to json

将 plist 转换为 json

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

Convert json to plist

将 json 转换为 plist

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